Question

I have a page with a search form. When you visit the page for the first time, I have a jQuery(document).ready() firing:

jQuery(document).ready(
  function () {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

Now when you submit the form and then hit the back button, the document.ready does not fire, and it needs to.

The original problem was only in Firefox, but after some research I found that adding an unload event to the body solved that:

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

Now, I've been told it does not work in IE11.

Anyone have any tips for getting the document.ready function to fire in IE11?

Was it helpful?

Solution

So, I stumbled across this answer in my search, buried at the bottom of the post.

I gave it a shot and it works:

// break the bfcache (ie11 and all others likely)
jQuery(window).focus(
  function() {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

I left the original code intact, because in my case I am merely setting a unix timestamp and it's OK if it happens multiple times.

Complete answer:

jQuery(document).ready(
  function () {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

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

jQuery(window).focus(
  function() {
    jQuery("#new_search").val(Math.round(new Date().getTime() / 1000));
  }
);

OTHER TIPS

For anyone else who runs into this problem, here is an MSDN article documenting how to stop IE11's backward/forward caching behavior: https://msdn.microsoft.com/library/dn265017(v=vs.85).aspx. According to that, adding a beforeunload event handler stops it. This also works on Firefox: https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top