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.
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
Looks like
document.referrer
doesn't work for navigating back. One way would be using one actual page and usinghistory.pushState
, so that you are able to keep the URLs in memory and using "different" pages at the same time. – pimvdb Commented Aug 17, 2012 at 12:20
2 Answers
Reset to default 5document.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:"/"});
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/en-US/docs/DOM/Manipulating_the_browser_history