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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With