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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…