Ajax, back button and DOM updates

JavascriptAjaxFirefox

Javascript Problem Overview


If javascript modifies DOM in page A, user navigates to page B and then hits back button to get back to the page A. All modifications to DOM of page A are lost and user is presented with version that was originally retrieved from the server.

It works that way on stackoverflow, reddit and many other popular websites. (try to add test comment to this question, then navigate to different page and hit back button to come back - your comment will be "gone")

This makes sense, yet some websites (apple.com, basecamphq.com etc) are somehow forcing browser to serve user the latest state of the page. (go to http://www.apple.com/ca/search/?q=ipod, click on say Downloads link at the top and then click back button - all DOM updates will be preserved)

where is the inconsistency coming from?

Javascript Solutions


Solution 1 - Javascript

One answer: Among other things, unload events cause the back/forward cache to be invalidated.

Some browsers store the current state of the entire web page in the so-called "bfcache" or "page cache". This allows them to re-render the page very quickly when navigating via the back and forward buttons, and preserves the state of the DOM and all JavaScript variables. However, when a page contains onunload events, those events could potentially put the page into a non-functional state, and so the page is not stored in the bfcache and must be reloaded (but may be loaded from the standard cache) and re-rendered from scratch, including running all onload handlers. When returning to a page via the bfcache, the DOM is kept in its previous state, without needing to fire onload handlers (because the page is already loaded).

Note that the behavior of the bfcache is different from the standard browser cache with regards to Cache-Control and other HTTP headers. In many cases, browsers will cache a page in the bfcache even if it would not otherwise store it in the standard cache.

jQuery automatically attaches an unload event to the window, so unfortunately using jQuery will disqualify your page from being stored in the bfcache for DOM preservation and quick back/forward. [Update: this has been fixed in jQuery 1.4 so that it only applies to IE]

Solution 2 - Javascript

I've been trying to get Chrome to behave like Safari does, and the only way I've found that works is to set Cache-control: no-store in the headers. This forces the browser to re-fetch the page from the server when the user presses the back button. Not ideal, but better than being shown an out-of-date page.

Solution 3 - Javascript

Facebook remembers page state by modifying the hash identifier in the URL for ajax requests. These changes are recorded in browser history, so when the user clicks the back button, the hash changes to what it was before. So then it is implied that you will need some Javascript to monitor the has identifier and react when it is changed by the browser. Andreas Blixt has a hash monitoring script available.

Solution 4 - Javascript

This has nothing to do with the hash (#) symbol.

If you would check apple's HTTP headers, it's simply caching the page.

Solution 5 - Javascript

Using the URL hash/fragment identifier is a pretty common way to hook/remember state in a web application that relies on Ajax and DOM updates.

Check out the Really Simple History project for some ideas. It's possible to monitor the URL for changes to the hash, and rsh does this, taking into account browser differences.

Solution 6 - Javascript

For anybody running in problems with Rails and this -- your issue isn't bfcache (I thought it was) -- it's the turbolinks gem. Here is how to remove it.

Hopefully this'll save you some time and banging your head against the wall.

Solution 7 - Javascript

What you are looking for is for some type of URL hash management. The # in the url is for client side only.

When you change the state of the back with JS, then you update the data in the # of the url.

Also you add some type of polling that monitors if the hash has changed, and loads the state of the page based off the new data in the hash.

Take a look at this:

http://ajaxpatterns.org/Unique_URLs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlubos haskoView Question on Stackoverflow
Solution 1 - JavascriptMilesView Answer on Stackoverflow
Solution 2 - JavascriptnornagonView Answer on Stackoverflow
Solution 3 - JavascriptJosh StodolaView Answer on Stackoverflow
Solution 4 - JavascriptLuca MatteisView Answer on Stackoverflow
Solution 5 - JavascriptPeterView Answer on Stackoverflow
Solution 6 - JavascriptYuval KarmiView Answer on Stackoverflow
Solution 7 - JavascriptBigBlondeVikingView Answer on Stackoverflow