How to get the previous page URL using JavaScript? [duplicate]
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.
Solution 1:
You can use the following to get the previous URL.
var oldURL = document.referrer;
alert(oldURL);
Solution 2:
<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:
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