4

Why does Firefox 5.0.1 not refresh this page when the back button is pressed?

<html>
<head>
  <meta http-equiv="cache-control" content="no-cache">
  <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>   
  <script>
  $(document).ready(function() { alert('ready'); });
  $(window).load(function() { alert('load'); });
  </script>
</head>
<body>
  <form action="http://www.google.com" method="get">
  <input name="q" type="text"> 
  <input type="submit">
  </form>
</body>
</html>

Steps to reproduce:

  1. Click "Submit Button"
  2. Press Back
  3. Neither document.ready or window.load fires

Update:

This will to force firefox to reload the page:

$(window).unload(function() {});

But, I ended up using this:

window.addEventListener('pageshow', function() {alert('pageshow'); }, false);
Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
  • +1 Firefox doesn't reload any resources and doesn't fire any callbacks when "Press Back". I've debug it with firebug – Anatoly Jul 25 '11 at 04:59
  • $(document).ready() seems to work for me... at least in a jsfiddle iframe -> http://jsfiddle.net/qwRPS/ – Tomm Jul 25 '11 at 04:59
  • @Tomm yes ready fires for me in on your jsfiddle.net example, now how to get my standalone example to work. – Justin Tanner Jul 25 '11 at 05:04

2 Answers2

9

Why does Firefox 5.0.1 not refresh this page when the back button is pressed?

Because it doesn't need to.

When you go "back" in Firefox and Firefox can load the page entirely from the back button buffer, then what you are going back to is not to a fresh page but to the page in the exact state you left it before as if nothing had happened in between, and that includes the state of all your Javascript code and variables. As far as your script (and jQuery) is concerned, ready() has already fired. jQuery will not fire it a second time if it's already fired.

What you probably want is to listen to the 'pageshow' event, which Firefox fires when the page is re-loaded from the buffer, even if its state is fully preserved.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • You read my mind, pageshow worked for my real purpose (re-enabling submit buttons, that I disabled) – Justin Tanner Jul 25 '11 at 06:04
  • 1
    It really depends upon what you want. Sometimes you want Firefox to just restore your state entirely (in which case you probably don't even need to do anything for pageshow). Or, sometimes you may want to re-initialize the page from scratch (in which case you want to bypass Firefox's state caching). The important thing is to consciously decide which is right for your page and design for that. – jfriend00 Jul 25 '11 at 06:07
1

See Ajax, back button and DOM updates and Restore object classes on back button in Firefox about Firefox's page cache that stores the entire state of the page and does not fire load events in most cases. If you want to avoid having Firefox save your page state (which depends upon what kind of state you have in the page), it looks like you can get it to skip it by registering an unload event for the page. Because an unload event could invalidate the page state, Firefox doesn't cache the page state if you have one and the back button will load the page freshly on the back button and all normal load events will fire.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    This will slow down the back button and it'll mean that you won't be going back to the page in the same state you left it particularly if you've interacted with elements on the page. This breaks the user experience a little bit. Doing this shouldn't be necessary if you listen for the pageshow event. – thomasrutter Jul 25 '11 at 05:55