How to get the previous page URL using JavaScript?
JavascriptJavascript 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