After travelling back in Firefox history, JavaScript won't run

JavascriptFirefox

Javascript Problem Overview


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>
  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>

Javascript Solutions


Solution 1 - Javascript

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 (Archived Version)

Solution 2 - Javascript

> 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!');
};

Solution 3 - Javascript

You can check the persisted property of the pageshow event. It is set to false on initial page load. When page is loaded from cache it is set to true.

window.onpageshow = function(event) {
    if (event.persisted) {
        alert("From bfcache");
    }
};

For some reason jQuery does not have this property in the event. You can find it from original event though.

$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        alert("From bfcache");
    }
});

Solution 4 - Javascript

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>

Solution 5 - Javascript

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

It should trigger onFocus instead based from this link here.

Solution 6 - Javascript

A simple way to cause a page to execute JavaScript when the user navigates back to it using browser history is the OnPopState event. We use this to pause and replay the video on our home page (https://fynydd.com).

window.onpopstate = function() {

    // Do stuff here...
};

Solution 7 - Javascript

In my case window.onunload with an empty function didn't help (I tried to set a value for dropdown when user uses backwards button). And window.onload didn't work for other reason - it was overridden by <body onload="...">.

So I tried this using jQuery and it worked like a charm:

$(window).on('pageshow', function() { alert("I'm happy"); });

Solution 8 - Javascript

for some cases like ajax operations url change listener can be used

$(window).on('hashchange', function() {
    	....
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatonzaView Question on Stackoverflow
Solution 1 - Javascriptuser65663View Answer on Stackoverflow
Solution 2 - JavascriptbobinceView Answer on Stackoverflow
Solution 3 - JavascriptMika TuupolaView Answer on Stackoverflow
Solution 4 - JavascriptChris HaasView Answer on Stackoverflow
Solution 5 - JavascriptsystempuntooutView Answer on Stackoverflow
Solution 6 - JavascriptMichael A.View Answer on Stackoverflow
Solution 7 - JavascriptShalguevView Answer on Stackoverflow
Solution 8 - JavascriptTamerView Answer on Stackoverflow