History API: Juststate Pushstate, get previous URL

$(window).bind('statechange',function(){ var State = History.getState(), url = State.url; }); 

In the following function, url returns the current URL. What I'm looking for in this function is to get the previous URL, for example, if I am on /cars/ferrari and go to /cars/ford , url will return cars/ford , but I want get /cars/ferrari .

How to get previous url in statechange event?

+4
source share
3 answers
 $(window).bind('statechange',function(){ // Prepare Variables var State = History.getState(), url = State.url, states = History.savedStates, prevUrlIndex = states.length - 2, prevUrl = states[prevUrlIndex].hash; }); 

It sounds like a trick! prevUrl gives me the desired URL.

+11
source

All I would do is click the URL on the status object so that you can return it back.

So, when you use pushState:

 history.pushState( {'previous_url': document.location.href}, document.title, "?some_query_string" ); 

Then, when you get the state, it will have the previous_url :)

+1
source

Try the following:

 alert(document.referrer); 

Note:

The value is an empty string if the user navigated to the page directly (not through a link, but, for example, through a bookmark). since this property returns only a string, this does not give you DOM access to the link page.

More details here: https://developer.mozilla.org/en-US/docs/DOM/document.referrer?redirectlocale=en-US&redirectslug=document.referrer

-1
source

Source: https://habr.com/ru/post/1202955/


All Articles