Thursday, April 17th, 2008
Are you sure your unload handler is firing in IE?
Johan Sörlin found that sometimes his unload event never fired in IE:
We recently found a serious bug in IE where the unload event wouldn’t fire on a specific page we had on a site. After some bug tracking we found out that the unload event never fired since all the contents of the page hadn’t finished loading before we navigated to another page.
This is a major problem since the unload event is commonly used to clear circular references etc in IE to prevent memory leaks. So this bug makes all Ajax libraries/frameworks out there that depend on the unload event on IE to fail if the page is unloaded before the contents of the page finished loading.
Here is an example of the bug, run the page in IE and follow the instructions on the page.
He then produced a work around:
-
-
function fixUnload() {
-
// Is there things still loading, then fake the unload event
-
if (document.readyState == 'interactive') {
-
function stop() {
-
// Prevent memory leak
-
document.detachEvent('onstop', stop);
-
-
// Call unload handler
-
unload();
-
};
-
-
// Fire unload when the currently loading page is stopped
-
document.attachEvent('onstop', stop);
-
-
// Remove onstop listener after a while to prevent the unload function
-
// to execute if the user presses cancel in an onbeforeunload
-
// confirm dialog and then presses the stop button in the browser
-
window.setTimeout(function() {
-
document.detachEvent('onstop', stop);
-
}, 0);
-
}
-
};
-
-
function unload() {
-
alert('Unload event occured.');
-
};
-
-
window.attachEvent('onunload', unload);
-
window.attachEvent('onbeforeunload', fixUnload);
-
In other IE news, remember not to have a CSS class that uses the (valid) _ character as IE 6 won't be happy.












Am I mistaken, or does jQuery already code around this bug?
Just tested this in IE7 WinXP and the unload event fired normally after I broke off the page loading before the image appeared.
So this seems to be one of those agonising “sometimes” bugs. I still have to test the load and unload events in my ongoing events research, so I’ll test explicitly for this bug.
jQuery only listens for the unload event on that line. And since that event never fires if the document never finished it’s loading it can’t cleanup the references. This is a common pattern in libraries.
Oops, sorry, commented without reading the bug instructions well enough. In my current IE7 the bug occurs as described in the quote.
Anyone have more details on this IE6 underscore business? I use underscores in css classes all the time and don’t seem to have a problem.
Aye, I get this problem as well — not only that, but in some weird cases with iFrames, not only will the unload handler not fire, but sometimes code set with SetInterval keeps firing after the iFrame’s been removed…
Actually, the CSS part would be the _ as the first character of a class name and no, you can’t use - either. But you will be able to use _ when IE6 dies.
And the unload bug… yep, it’s a tough one!
I am currently using Google Maps for a number of projects and unfortunately keep running into issues with the inability to use th unload event. Any suggestions on fixing/patching/repairing such a thing?