BFcache disabled by unload events (Back-Forward cache)
browser-cache, caching, firefox, javascript, jquery
Solution
In order to enable BFCache you need to remove `beforeunload` event listener. It should be the same listener which was added by Piwik code, otherwise removeEventListener will do nothing.
That listener is unreachable outside of the Piwik's source, so one does not simply remove it.
But, if you have a possibility to insert code before the Piwik, you can try to override `addEventListener`, track added handlers and expose function to remove all tracked handlers at once.
Problem
(Note: FireFox only) The Back-Forward cache is a caching system in firefox that runs when the back button is clicked. It will then simply use the DOM from the previous page that is in it's cache instead of reloading the entire page (and re-requesting files). I'm using piwik (an analytics service), that requires a tracking code snippet to be added to the footer. Upon adding this, the back-forward cache no longer works. It is my understanding that, if there is an unload event (or beforeunload) the bfcache is automatically disabled. This is likely what is happening here. Is there anything I can add to make the BFCache work anyway? To make matters worse, I cannot add any custom code below the piwik code. That one is always last. I added the code displayed below to try and remove any unload events that are registered, but the BFcache is still not working. ``` $(window).unbind('beforeunload'); $(window).unbind('unload'); window.onbeforeunload = null; window.onunload = null; ``` I also tried: ``` function UnloadHandler() { window.removeEventListener('unload', UnloadHandler, false); } window.addEventListener('unload', UnloadHandler, false); $(window).unload(function () { $(window).unbind('unload'); }); ``` but this too does not work. I have placed some samples online. Remember to test this with Firefox: this one shows a working BFcache (you will get an different alert based on whether or not the back button was clicked) http://users.telenet.be/prullen/bfcache/a.html Loaded piwik, BFCache no longer works http://users.telenet.be/prullen/bfcache/b.html Loaded piwik, tried to unset onload event, but still not working http://users.telenet.be/prullen/bfcache/c.html Using unloadhandler http://users.telenet.be/prullen/bfcache/d.html Suggestions by @roasted http://users.telenet.be/prullen/bfcache/e.html http://users.telenet.be/prullen/bfcache/f.html More information about BFCache: https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching You can see another demo of the behavior here: http://www.twmagic.com/misc/cache.html If you add dom elements, and click the first link, then return - the dom elements are still there. However, if you add an onload or beforeunload event that is not the case. Again, test this in firefox. Any ideas?