Pages modified with AJAX: how to restore the previous DOM state after browser history "back" button?
I'm modifying a part of the DOM of a HTML page with AJAX (only the middle container of the page, not the header and footer which are fixed), more precisely with something like:
fetch(url).then(r => r.text()).then(html => {
container.outerHTML = html;
history.pushState({ url: url }, "", url);
});
when a button is pressed. The URL changes successfully in the browser URL bar thanks to pushState
.
Problem: when I go back in history with the browser "Back" button, then it doesn't restore the previous state of the DOM.
Question: Is there a way to use pushState
so that it automatically saves the DOM state when it is called, and it automatically retrieves the DOM state when we navigate in history, for example with the "Back" button? Or do we have to do this manually?
Note: I have read MDN Ajax navigation example but it does not give a clear answer to this. Also the question onpopstate handler - ajax back button is linked but not about the saving/restoring of the DOM.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/rvyiqQR
Comments
Post a Comment