I understand that a checkbox will remain checked when you return to a page via the back button. Classes added using jquery's addClass however do not. Can someone help me understand when something will persist or not when returning via the back button? Also, is there a way to save the value of a variable so that I can use the variable values to recreate the objects that did not persist?
A back button in the browser lets you back-up to the copies of pages you visited previously. The web browser's back and next buttons work well with web sites that provide information that changes infrequently, such as news and shopping web sites.
Back button (web browser), a common web browser feature that retrieves the previous resource. Backspace key, the computer keyboard key that deletes the character(s) to the left of the cursor.
The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history. forward() in JavaScript). Browsers tend to handle the popstate event differently on page load.
Nothing persists - classes don't persist because DOM is regenerated, JS variables don't persist because code gets re-executed etc. At the same time, browser implementations seem to pretty much agree to keep user-entered form data for convenience even though this behaviour is not part of any official spec.
If you require some data to persist on the client-side - chances are you're doing something wrong architecturally. Why are you trying to override browser "back" button behaviour for the user to be taken back to an arbitrary state, rather than going to the previous URL and re-rendering the page? If your app is interaction-heavy and this "back" button behaviour is desired, you may want to back away from having the user navigate between independent pages in favour of making AJAX requests and relying on something like html5 history api allowing you to execute any arbitrary code to put the page into a desired state.
You don't go into enough details about what you actually expect to be store - maybe a localStorage or a cookie alternative would be more appropriate
Update:
I've put together a fiddle to test how well form data will persist when traversing history, and hidden fields were available to be processed by JS - still not a good idea though imo
Modern browsers try to persist more and more. Firefox experimented several years ago with preserving everything including class name changes, as long as the server indicates that the page is cachable in the HTTP header and the webpage has not registered any onunload
event handlers. Firefox fires onpageshow
and onpagehide
when the user presses back.
https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
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