[javascript] How to get the previous page URL using JavaScript?

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.

This question is related to javascript

The answer is


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


<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.


You can use the following to get the previous URL.

var oldURL = document.referrer;
alert(oldURL);