firefox not executing javascript on back button

6 בספטמבר 2011

Hi,


firefox has a cache behavior that uses in-memory caching for entire Web pages, including their JavaScript states, for a single browser session.


This can cause to unexpected javascript and HTML behavior when pressing on the browser back button.


For example:




  1. PageLoad will not fired


  2. javascript code will not executed


  3. DHTML that was created by JavaScript will be shown

and more…


In case firefox cache is something you want to prevent, Below are some common programmatic reasons that a page will not be cached by firefox:


  • the page uses an unload or beforeunload handler

  • the page sets "cache-control: no-store"

  • the page sets "cache-control: no-cache" and the site is HTTPS.

  • the page is not completely loaded when the user navigates away from it

  • the top-level page contains frames that are not cacheable

  • the page is in a frame and the user loads a new page within that frame (in this case, when the user navigates away from the page, the content that was last loaded into the frames is what is cached)

  • Code example for prevent page caching on firefox:


    window.addEventListener('pageshow', PageShowHandler, false);
    window.addEventListener('unload', UnloadHandler, false);

    function PageShowHandler() {
    window.addEventListener('unload', UnloadHandler, false);
    }

    function UnloadHandler() {
    window.removeEventListener('unload', UnloadHandler, false);
    }


    You can read more here.


    Rotem

    Add comment
    facebook linkedin twitter email

    Leave a Reply to Avi Pinto Cancel Reply

    Your email address will not be published.

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

    *

    2 comments

    1. Avi Pinto18 בספטמבר 2011 ב 9:28

      Great summary of it thanks 🙂

      think you have the wrong example there –

      the code sample you showed is used(as said in the Mozilla link) to preserve the cache "If you want to use unload or beforeunload events and also ****preserve**** page caching, then you can simply remove these events in the event handler function and reinstate them in an pageshow handler function if you return to that page"

      so maybe all you need is:

      window.addEventListener('unload', UnloadHandler, false);

      function UnloadHandler() {

      //do nothing

      }

      Reply
    2. Rotem Bloom19 בספטמבר 2011 ב 16:28

      Yes Avi,
      You are right the unload event is enough.

      Rotem

      Reply