JavaScript supposed to always be called on front page, but doesn't always run I have some JavaScript that is called on the front page, and is supposed to execute.  When I go the page initially, it works fine.  Then if I go to another page, then come back, it doesn't show.  It does load, and after other JavaScript files it requires.  It just doesn't execute.  If I refresh, it loads again.

(function ($) {


    Drupal.behaviors.chartSavings = {
        attach: function (context, settings) {

            // .....

            // create the submit handler
            $('#savings-form').submit(function(event) {

                event.preventDefault();
                //alert('Form submitted xxx');

                savingsplot();

            });

            savingsplot();

        }
    }

})(jQuery);


What am I doing wrong?  I don't have any caching enabled, and in drupal_add_js(), I set cache to FALSE.
I think perhaps the issue stems from the browser's "BFCache" which is a browser feature that caches a page for quicker loading when a user presses the Back/Forward button. See below to understand more about this feature.

Try adding the following code inside of your attach function.

// Prevent the bfcache.
$(window).bind("unload", function() {});


The "BFCache" as taken from page:
https://stackoverflow.com/a/2218733/955858

To quote that post:


  The modern browsers (Firefox, Safari/Chrome, and Opera) all support the special "back/forward" cache (I'll call it bfcache, which is a term invented by Mozilla), involved when the user navigates Back. Unlike the regular (HTTP) cache, it captures the complete state of the page (including the state of JS, DOM). This allows it to re-load the page very quickly and exactly as the user left it, which is what the user wants.
  
  The load event is not supposed to fire when the page is loaded from this bfcache. For example, if you created your UI in the "load" handler, and the "load" event was fired once on the initial load, and the second time when the page was re-loaded from the bfcache, the page would end up with duplicate UI elements.
  
  This is also why adding the "unload" handler stops the page from being stored in the bfcache (thus making it slower to navigate back to) -- the unload handler could perform clean-up tasks, which could leave the page in unworkable state.
  
  For pages that need to know when they're being navigated away/back to, Firefox 1.5+ and some next version of Safari (which contains the fix for bug 28758) supports special events called "pageshow" and "pagehide".
  
  References: http://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/ and https://developer.mozilla.org/En/Using_Firefox_1.5_caching.