Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop the last state from history

Is it possible to get the html of the last state pushed to history stack, something like this:

history.pushState(null, null, {html: $('body').html()});

Change the window location:

window.location = url;

Now from the new location:

window.addEventListener('popstate', function(event) {
    // back to the last state - event.state.html
}
history.back() // That what's firing the popstate event? 
like image 462
shmnsw Avatar asked Apr 17 '15 18:04

shmnsw


People also ask

How do I delete my state history?

To solve this problem you can call window. history. back(); to remove the history entry by yourself which actually deletes the state as requested. Alternatively you could push a URL into the history that holds an anchor, e.g. #editor and then push to history or not if the anchor exists in the recent URL or not.

What is state pushState history?

The state object is a JavaScript object which is associated with the new history entry created by pushState() . Whenever the user navigates to the new state , a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

What is history replaceState?

The History. replaceState() method modifies the current history entry, replacing it with the state object and URL passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.


1 Answers

I searched for the request to get the html of the last state pushed to history, but did not get satisfactory answers. But I have an idea for you, why dont you try to save the html of the state before pushing it to history? This is quite possible using client side (cookies) or server side (session) variables, which can be retrieved even after the things have been pushed back in history.

like image 152
Victor Juliet Avatar answered Sep 22 '22 01:09

Victor Juliet