3

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.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Pradip Prajapati
  • 152
  • 1
  • 4
  • 18
  • 1
    Looks like `document.referrer` doesn't work for navigating back. One way would be using one actual page and using `history.pushState`, so that you are able to keep the URLs in memory and using "different" pages at the same time. – pimvdb Aug 17 '12 at 12:20

2 Answers2

5

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:"/"});
Community
  • 1
  • 1
Lex Eichner
  • 1,056
  • 3
  • 10
  • 35
4

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

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62