Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what will persist when returning via back button?

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?

like image 635
user584583 Avatar asked Oct 04 '12 21:10

user584583


People also ask

What happens when we press back button in browser?

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.

What is the Back button used for?

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.

What event is fired when the back button of a browser is pressed?

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.


2 Answers

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

like image 114
Oleg Avatar answered Nov 15 '22 23:11

Oleg


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/

like image 37
Kernel James Avatar answered Nov 15 '22 22:11

Kernel James