In Chrome, FF, and IE8-10, when I press the back button, my javascript $(document).ready() function is called, but in IE11, none of the javascript is invoked. Does anyone know how to make IE11 respond like all the other browsers and bring consistency to my code?
<script type="text/javascript">
alert("Are we called?"); // neither is this called in IE11
$( document ).ready( function() {
alert("document ready"); // does not get fired after hitting back on IE11
});
</script>
The annoying issue about IE11 is that if you turn on developer tools and start trying to trace or debug the issue, it goes away and it behaves as IE10 and calls the .ready() after going back.
I do have cache disabled on this page via this header and again, it works on all other browsers that I am looking to support (IE8-10, FF, and Chrome).
Cache-Control: no-cache
You can-not actually disable the browser back button. However, you can do magic using your logic to prevent the user from navigating back which will create an impression like it is disabled.
The back button is a user interface feature that takes a user back to their previous location. For instance, in an Internet browser, clicking the back button takes you back to the last web page you visited.
Alright, after pounding away at the problem for the past 2 hours, here's my findings.
The following events are never called between back and forward:
IE11 caches everything on page including the javascript state so none of the usual events are fired. When going back and forth between a page via the back/forward buttons, javascript code just resumes state without being notified that it was interrupted. Sorta rude to developers imho or maybe there is an event that is triggered for this, but I certainly don't know about it.
Anyways, you can see this behavior on this page full of balls. Note in IE11, you navigate to google.com and then press back, all the balls are in the same exact location and everything continues to work. In every other browser, the page is reinitialized and the the balls drop fresh from the ceiling again. I can see scenarios where IE11 behavior would be useful and beneficial, but I just wish Microsoft would provide documentation for when you don't want it like that. Also, it would be nice if the defaults would be like every other browser and making this feature optional instead of breaking all compatibility with everything, including its own IE9 and IE10!
So with that, I realized that if I started a timer, it would just pick off from where I left off. I don't like this code as it is a busy-wait hack, but it does what I want. It would be great if someone could think of something better that wouldn't be so busy...
<!-- IE11 back/forward hack - http://stackoverflow.com/questions/21274085/internet-explorer-11-back-button-javascript-behavior -->
<script type="text/javascript">
var dumbIEHistory=history.length;
function busyWaitCheckForHistoryChange() {
if (history.length != dumbIEHistory) {
alert("History has changed, back/forward has been pressed, run my function!");
myFunction();
dumbIEHistory=history.length;
}
}
// using http://www.pinlady.net/PluginDetect/Browsers/ to do browser detection
$( window ).load(function() {
if (browser.isIE && browser.verIE >= 11) {
// let's not bog down all the other browsers because of IE11 stupidity
window.setInterval("busyWaitCheckForHistoryChange()", 1000);
} else {
// in other browsers, this will be called when back/forward is pressed
myFunction();
}
});
</script>
Works for what I'm doing to catch when the back/forward button is pressed because the history length will be +1 after we hit back. If the user navigates back and forth too quickly, there might be a split second before the function is called, if that is a concern, you can reduce the interval. Hope this helps others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With