onbeforeunload - Blocks Caching?
Here’s one I hadn’t seen, via Ajaxian:
window.onbeforeunload = function () { // This fucntion does nothing. // It won't spawn a // confirmation dialog // but it will ensure that // the page is not cached // by the browser. }
Just having an unbeforeunload event handler — regardless of whether or not it actually does anything, regardless of whether or not you spawn a dialog box or not, even if the entire function declaration consists entirely of just { } — just defining an event handler will prevent the page from being cached — ever. As a matter of fact, even if you allow page caching, the page will be not be cached. Having an onbeforeunload event means the page will be re-built every single time it is accessed. Javascripts will re-run, server-side scripts will be re-run, the page will be built as if the user was hitting it for the very first time, even if the user got to the page just by hitting the back or forward button.
technorati tags:javascript, caching, ajaxian
August 14th, 2007 at 6:02 am
I think this is incorrect. Here was my experiment:
(1) Create a minimal page with the onbeforeunload event and a link.
(2) Visit that page in your browser.
(3) Edit the page on the server by inserting a word or something.
(4) Click on the link
(5) Press the browser’s “back” button.
In both IE7 and FF2 it goes back to the original version, i.e. its old cached version, the one without the inserted word.
August 14th, 2007 at 9:15 am
I think this would prevent caching on the next trip in, i.e. visiting the page -> navigating away -> visiting the page again (via typing the URL into the address bar or navigating there through a link).
That’s what this guy seems to be saying.