78

How do I get the URL of previous page in JavaScript?

Say, I go from page A to B, and use browser back button to go back to page A.

I've tried to use history.previous, but I'm can't get it to work.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Gaglad
  • 797
  • 1
  • 5
  • 3
  • 6
    [`history.previous` is a proprietary extension of Mozilla (and not available to web content)](https://developer.mozilla.org/en/DOM/window.history). In any case, I don't think that it is possible, this would be a huge security issue. – Felix Kling Apr 26 '11 at 08:59
  • 1
    Thinking about it.... it is probably not a *huge* security issue, but I don't want JavaScript to be able to read my browser history ;) – Felix Kling Apr 26 '11 at 09:05
  • It is a big security issue because of the stuff some sites put in query strings i.e. some cookie-less session and even authentication techniques – eglasius Sep 27 '12 at 07:58
  • 1
    @Gaglad I got the same problem here. `document.referrer` is not the one we need if we click the `back` button – Huei Tan May 05 '15 at 06:48
  • if you don't like document.referrer Then maybe you can use cookies? Keep track of the previous page, by setting to the current page after all of your script has run – Jomar Sevillejo Oct 30 '15 at 00:13
  • It would be nice to have in some debugging context, that wasn't available in general. Right? Hello? Mozilla and Google designers scan SO comments for feature ideas all the time, right? – WiseOldDuck May 17 '17 at 00:14

3 Answers3

117

You can use the following to get the previous URL.

var oldURL = document.referrer;
alert(oldURL);
jotik
  • 17,044
  • 13
  • 58
  • 123
Ammu
  • 5,067
  • 9
  • 34
  • 34
  • 3
    i tried document.referrer, but it gives the last page url only if we use a back button in Page B, when i use Browser back button am not getting the previous url - Gaglad – rv_k Apr 26 '11 at 09:17
  • This worked like a charm for my purposes. I needed to know the previous page so I could log that and know what page someone was giving feedback about. Thanks!! – Noah Gary Aug 17 '15 at 22:33
  • 1
    previous is not a referrer – Green Nov 20 '15 at 09:02
  • 2
    It can only get the **HOSTNAME** of the previous url – Some Guy Jan 28 '20 at 09:33
15
<script type="text/javascript">
    document.write(document.referrer);
</script>

document.referrer serves your purpose, but it doesn't work for Internet Explorer versions earlier than IE9.

It will work for other popular browsers, like Chrome, Mozilla, Opera, Safari etc.

Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
565
  • 615
  • 2
  • 10
  • 19
8

You want in page A to know the URL of page B?

Or to know in page B the URL of page A?

In Page B: document.referrer if set. As already shown here: How to get the previous URL in JavaScript?

In page A you would need to read a cookie or local/sessionStorage you set in page B, assuming the same domains

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • i tried document.referrer, but it gives the last page url only if we use a back button in Page B, when i use Browser back button am not getting the previous url - Gaglad – rv_k Apr 26 '11 at 09:16
  • 1
    @rv_k You are not being clear on what "previous" url means. If you use the back button you cannot tell where you came from in the page you go to when you click it. There is no way. – mplungjan Apr 26 '11 at 09:19
  • I have a link in Page A to B, only possible way to get to page A from page B is using browser back button and i have to do a script if so .So i have to check the url of previous page to check whether its page B. - Gaglad – rv_k Apr 26 '11 at 09:34
  • So in page A you need to see if the user came back from B. It is only possible to know if you have BEEN to B, not if the previous page your visited was B. You might want to consider a frameset or to use location.replace("b.html") to remove A from the history if that is your need – mplungjan Apr 26 '11 at 09:36
  • no i have to send a value from cookies to a jquery plugin function if the user came back from B to Page A - Gaglad. – rv_k Apr 26 '11 at 10:00
  • 1
    So you need to set a cookie in page B to tell page A it has been visited. Nothing else to do in my opinion - perhaps you can test the "visited" state of the link to B – mplungjan Apr 26 '11 at 13:05
  • for privacy/security reasons chrome and FF prevent testing visited state of links – pilavdzice Nov 25 '14 at 22:17
  • Tested on your own post and it's working. I clicked on the link you have provided, then opened the console. ``document.referrer`` returns ``"http://stackoverflow.com/questions/5788108/how-to-get-the-previous-page-url-using-javascript"`` – vikkee May 08 '17 at 16:33