How to get the previous page URL using JavaScript?

Javascript

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

You can use the following to get the previous URL.

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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: https://stackoverflow.com/questions/3528324/how-do-you-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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGagladView Question on Stackoverflow
Solution 1 - JavascriptAmmuView Answer on Stackoverflow
Solution 2 - Javascript565View Answer on Stackoverflow
Solution 3 - JavascriptmplungjanView Answer on Stackoverflow