Coder Perfect

In JavaScript, how do I access the previous URL?


Is it possible to retrieve the previous URL with JavaScript? Something along these lines:

alert("previous url is: " + window.history.previous.href);

Is there anything similar? Is it better to just keep it in a cookie? I only need to know so that I can make seamless transitions from one URL to the next without using anchors or anything.

Asked by Lance

Solution #1


If the user arrived at the current page by clicking a link (rather than typing straight into the address bar, or, I suppose, in certain situations, by submitting a form? ), you’ll often obtain the URL of the last page they visited. DOM Level 2 specifies this. More information can be found here.

For security and privacy reasons, window.history permits navigation but not access to URLs in the session. If a more thorough URL history was available, every site you visited would be able to see everything you’d done previously.

If you’re dealing with state movement on your own site, one of the standard session management solutions, such as cookie data, URL params, or server side session information, is perhaps less brittle and surely more useful.

Answered by Ben Zotto

Solution #2

The new History api can be used to return to a previous page without knowing the URL.

history.back(); //Go to the previous page
history.forward(); //Go to the next page in the stack
history.go(index); //Where index could be 1, -1, 56, etc.

However, if your browser doesn’t support the HTML5 History API, you won’t be able to change the content of the history stack.

See the document for further information.

Answered by RPDeshaies

Solution #3

You can perform the following if you’re developing a web app or single page application (SPA) where routing takes place in the app/browser rather than a round-trip to the server:

window.history.pushState({ prevUrl: window.location.href }, null, "/new/path/in/your/app")

Then, in your new route, you can obtain the prior URL by doing the following:

window.history.state.prevUrl // your previous url

Answered by Jonathan Lin

Solution #4

In most cases, document.referrer is not the same as the real URL.

I’m working on an application that requires me to create a frameset with two frames. One frame is well-known, and the other is the page from which I’m linking. It appears that document.referrer would be great because you wouldn’t have to supply the frameset document the actual file name.

If you modify the bottom frame page later and then use history.back(), the old page is not loaded into the bottom frame; instead, document.referrer is reloaded, and the frameset is gone, and you are back to the original beginning window.

It took me a bit to figure this out. So document.referrer isn’t just a URL in the history array; it’s also the referrer window specification. At least, that’s the best I can figure it out right now.

Answered by kevin at KAB

Solution #5

<script type="text/javascript">

Although document.referrer meets your objective, it is incompatible with Internet Explorer versions prior to IE9.

Other popular browsers, such as Chrome, Mozilla, Opera, and Safari, will also operate.

Answered by 565

Post is based on