I want to restore an ajax entire modified page along with the javascript variables in memory when the browser back button is pressed. It was added to the brwoser history manually using window.history.pushState()
. I'm trying to save/restore the page using :
function changeURL(){
window.history.pushState(document.body.innerHTML,"","...");
}
window.onpopstate = function(e){
if(e.state){
document.body.innerHTML = e.state;
}
};
Seemingly this works but just a problem that the javascript variables that were in memory at the time page was loaded are no longer there as they are modified by subsequent ajax requests but I didn't made any provision to restore them on pressing back button. How can I restore all those values to the variables ?
localStorage
or any other mechanism you prefer. Edit: I'd also recommend not treating the entirety of document.body.innerHTML
as your serialized "state". It should be possible to isolate the variables that you are using to modify the HTML content, and use that as your state. Then you can just regenerate the corresponding HTML by re-applying your variables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.