Is it possible to get URL of previous page visited

2019-02-26 17:28发布

I am on page A.

Now I click in page A and redirected to page B. Now I click in page B and redirected to page A.

Here i want to know the url of page B.

I have tried document.referrer but its not working.

3条回答
够拽才男人
2楼-- · 2019-02-26 17:39

document.referrer works when a user clicks on a link to navigate to the current page but I don't think it works when you do a manual redirect in a Javascript function.

The only way I can think of is to store the URL of the page before the user is redirected to your current page.

You could store it in a hidden form or a cookie.

This question has also been asked before and its worth having a quick read through here. How do you get the previous url in Javascript?

I took this cookie example from it to show you what I mean.

$.cookie("previousUrl", window.location.href, {path:"/"});
查看更多
虎瘦雄心在
3楼-- · 2019-02-26 17:44

You can't know the URL of the previous page, but you can link to it using

window.history.back();

or

window.history.go(-1);

if you want to know if there is a previous page, check

window.history.length;

If it's more than 0, there is a prev page. See https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history

查看更多
4楼-- · 2019-02-26 17:48

You can use history object of javascript. Please refer the url http://www.w3schools.com/jsref/obj_history.asp

查看更多
登录 后发表回答