Quinthar

How to execute code whenever Back button is pressed?

So I'd like to execute some JavaScript every time the page loads, regardless of whether it's from a new visitor, or click of Reload, or a click of Back, or whatever.  However, I'm finding Back to be particularly tricky.  Here's the test code:

<html>
<body onload="alert('Hello world!');">
    <a href="link">link</a>
</body>
</html>
Nothing fancy there, should pop an alert box every time right?  Wrong: click on the link and then click Back, and the alert doesn't show.  (At least, not on Ubuntu 8.04 Firefox 3.)  Odd. 

Even stranger, it *does* show if you include a large (~100KB) external script:
<html>
<head>
    <script type="text/javascript" src="jquery.js"> </script>
</head>
<body onload="alert('Hello world!');">
    <a href="link">link</a>
</body>
</html>
But try it with a small (~4KB) external script and it stops working.  The mystery deepens.

The only explanation that makes sense is there must be some kind of delay when the page loads for the Back button to trigger the onload event.  Too small a delay (too small a file to load) and it doesn't work.

Any ideas how to reliably execute code on every load, including Back?  I'll use the jQuery trick for now, but I'm concerned that it won't always be reliable.



Update: Curtis figured it out: It's due to fancy Firefox caching rules. There are several criteria that go into whether the page is reloaded each time, one of which is whether there's an "unload handler". Turns out it wasn't the *size* of including jQuery, it was due to jQuery setting an unload handler. Anyway, of the many solutions, the easiest is to simply set "Cache-Control: no-store" (or "no-cache", if HTTPS). Thanks Curtis, mystery solved!

No comments:

- Jan 2014 (1) - Mar 2012 (1) - Nov 2011 (1) - Oct 2011 (1) - Apr 2011 (1) - Mar 2011 (3) - Feb 2011 (2) - Jan 2011 (9) - Nov 2010 (1) - May 2010 (1) - Mar 2010 (1) - Feb 2010 (1) - Jan 2010 (1) - Dec 2009 (1) - Nov 2009 (1) - Oct 2009 (1) - Sep 2009 (1) - Aug 2009 (2) - Jul 2009 (1) - Jun 2009 (4) - May 2009 (3) - Apr 2009 (3) - Mar 2009 (10) - Feb 2009 (5) - Jan 2009 (3) - Dec 2008 (5) - Nov 2008 (5) - Oct 2008 (5) - Sep 2008 (4) - Aug 2008 (5) - Jul 2008 (11) - Jun 2008 (8) - Feb 2008 (1) - Aug 2007 (1) -