Is it possible to get URL of previous page visited

2019-02-26 16:56发布

问题:

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.

回答1:

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:"/"});


回答2:

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



回答3:

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