views:

706

answers:

4

When I use the back button on Firefox to reach a previously visited page, scripts on that page won't run again.

Is there any fix/workaround to have the scripts execute again when viewing the page the second time?

Please note that I have tested the same pages on Google Chrome and Internet Explorer and they work as intended.


Here are the files and the steps I used to test the problem:

(navigate to 0.html, click to get to 1.html, back button)

0.html

<html><body>
<script type="text/javascript">
  window.onload = function() { alert('window.onload alert'); };
  alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>

1.html

<html><body>
<p>Go BACK!</p>
</body></html>
A: 

Wire in an "onunload" event that does nothing:

<html><body>
<script type="text/javascript">
  window.onload = function() { alert('window.onload alert'); };
  window.onunload = function(){}; 
  alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>
Chris Haas
Why was this downvoted?
SLaks
Not sure, I gave a up vote, looks like someone just downvoted every answer to this question...
Patonza
+2  A: 

Set an empty function to be called on window.onunload:

window.onunload = function(){}; 

e.g.

<html><body>
<script type="text/javascript">
  window.onload = function() { alert('window.onload alert'); };
  window.onunload = function(){};
  alert('inline alert');
</script>
<a href="1.html">Click Me!</a>
</body></html>

Source: http://www.firefoxanswer.com/firefox/672-firefoxanswer.html

fig
Thank you, it works.Any hint on what does the default onunload handler do? (e.g. am I overriding a default behaviour of some sort here?)
Patonza
Good question. I'd guess there probably simply isn't one, which leaves a flag that onload's already been fired still intact even after clicking the back button.
fig
Ok thanks, I'll investigate. Thank you again, this problem has been haunting me for a while :)
Patonza
anybody know why FireFox needs this while other browsers do not?
Pim Jager
You're not overriding anything, this just prevents Firefox from caching the page in the Back-Forward Cache (bfcache). https://developer.mozilla.org/en/DOM/window.onunload https://developer.mozilla.org/En/Using_Firefox_1.5_caching
Chris Haas
A: 

As far as i know Firefox does not fire onLoad event on back.

It should trigger onFocus instead based from this link here.

systempuntoout
Tested it and windows.onfocus DOES indeed get called, even without setting the empty window.onunload handler. Setting onunload is a slightly nicer workaround but .onfocus should be fine too. Thanks :)
Patonza
+3  A: 

When I use the back button on Firefox to reach a previously visited page, scripts on that page won't run again.

That's correct and that's a good thing.

When you hit a link in Firefox (and Safari, and Opera), it does not immediately destroy your page to go onto the next one. It keeps the page intact, merely hiding it from view. Should you hit the back button, it will then bring the old page back into view, without having to load the document again; this is much faster, resulting in smoother back/forward page transitions for the user.

This feature is called the bfcache.

Any content you added to the page during the user's previous load and use of it will still be there. Any event handlers you attached to page elements will still be attached. Any timeouts/intervals you set will still be active. So there's rarely any reason you need to know that you have been hidden and re-shown. It would be wrong to call onload or inline script code again, because any binding and content generation you did in that function would be executing a second time over the same content, with potentially disastrous results. (eg. document.write in inline script would totally destroy the page.)

The reason writing to window.onunload has an effect is that the browsers that implement bfcache have decided that — for compatibility with pages that really do need to know when they're being discarded — any page that declares an interest in knowing when onunload occurs will cause the bfcache to be disabled. That page will be loaded fresh when you go back to it, instead of fetched from the bfcache.

So if you set window.onunload= function() {};, what you're actually doing is deliberately breaking the bfcache. This will result in your pages being slow to navigate, and should not be used except as a last resort.

If you do need to know when the user leaves or comes back to your page, without messing up the bfcache, you can trap the onpageshow and onpagehide events instead:

window.onload=window.onpageshow= function() {
    alert('Hello!');
};
bobince
My problem is that bfcache does not cache ALL the page so I have to re-run js to rebuild lost content. So thrashing the whole page could be ok. Anyway, I'm not using the window.onload event, I'm using jQuery document.ready event. Do you know a way to still be able to use document.ready and avoid this problem?
Patonza
bfcache should in general return the entire page as it was when it was left. What is the content that's missing after returning from the previous page? (test case?) `document.ready` will work essentially in the same way as `window.onload` (for some browsers, they are the same event anyway).
bobince