JavaScript OnUnload event broken in IE6

[Update: This blog post sets up the problem, though it appears I was pointing the finger the wrong way. It turns out the problem was caused by Google Toolbar. See my followup post for more details.]

The JavaScript / JScript onunload event is notoriously unreliable from browser to browser. Reading though various USENET posts leads me to believe that the behaviour is slightly different between Mozilla derivatives, Opera, and IE. That sucks but is nothing new. What is, however, annoying is that behaviour differs on the same browser.

A few months ago I coded up some functionality that persisted state back to the server when the user left the page. I used the onunload event and the HTTPPost function. I tested it on XP and IE6 (my primary dev environment) and all was well. Recently, I started noticing that I don't get all state info I should be getting. I dug a little deeper and found that the onunload event is no longer firing when the browser window is closed by the user. It still fires if the page is refreshed or if the user navigates to another page. This is still on the same development machine with no other code changes involved. What gives? Well, the only thing I can think of is that in my diligence to apply every security patch Microsoft puts out, I've applied something that broke the onunload event. That is to say, Microsoft broke IE behaviour with respect to the onunload event. I say “broke” because their documentation states that to invoke the event, the user has to “Close the current browser window.” (this is the first item in a long list of actions that should trigger the event).

[rant]
This is the kind of breaking change that should be avoided, or at least damn well documented. If there is really a compelling reason to drop functionality (for security reasons or whathaveyou), please, PLEASE, tell us about it!!!

Today, because this is causing loss of data, I have to drop everything and try to figure out if there is a way to hack onunload behavior back in. My biggest fear is that I may have to completely rearchitecture the way this part of the application works. What's worse, is that even if Microsoft fixes this in the next WindowsUpdate patch, there will now be a (potentially large) pool of users for whom the current implementation will not work, making the onunload event forever useless to me.

Grrr!!!!
[/rant]

To test this functionality, copy and paste this code into a new file:

<html>
<head>
 <title>OnUnload Test</title>
</head>

<body onunload="alert('Event Fired');">

This is a test page.

</body>
</html>

 

<html>
<head>
 <title>OnUnload Test</title>
</head>

<body onunload="alert('Event Fired');">

This is a test page.

</body>
</html>


Open the file in IE. Hit F5, see the message. Navigate to another page, see the message. Close the browser window. If you see the message, please let me know what version of IE you are using...

update 1: I found this Scandinavian post on what seems like the same subject. If someone out there could translate the gist of it into English for me, I would really appreciate it!

update 2: I am working with Microsoft Support to investigate / resolve this issue.

update 3: It turns out the problem was caused by Google Toolbar (at version 2.0.110-big/en (GGLD) as of this writing). You can see my follow-up post for more details.

posted @ Wednesday, April 28, 2004 6:51 AM

Print

Comments on this entry:

# re: JavaScript OnUnload event broken in IE6

Left by paule at 11/26/2004 7:08 AM
Gravatar
hi,

as far as i know, ie behaves in this way because it's internal
popup-blocker "-> settings->security->stufe anpassen/change settings?->use popupblocker" is active by default.

sadly ie6 is not reporting having blocked any window, so i think it's behavior regarding "onunload" is really some kind of buggy.

all in all it makes "onunload" more and more unuseable, as far too many people are using microsofts ie.

all in all i think it's a good think, as it prevents fucked-up people from doing crazy things with this eventhandler.


bye

# re: JavaScript OnUnload event broken in IE6

Left by Michael Teper at 11/26/2004 10:00 AM
Gravatar
Paul, I have not observed this behavior from IE6 with the sp2 popup blocker.

# re: JavaScript OnUnload event broken in IE6

Left by Beto at 5/4/2005 1:14 PM
Gravatar
Hey man...
I was having a problem with the application I'm currently developing... I was using <body onUnload="verificaAlteracoes();">, and it wasn't working... so I created a new file, just with the basic tags, and the onUnload crap on the body tag :D, and it worked (IE 6). So I tried another solution, and I found a better event: onBeforeUnload.
<body onBeforeUnload="alert('Alanis Morissette rules');">
Try using it...

Beto.

# re: JavaScript OnUnload event broken in IE6

Left by Michael Teper at 5/4/2005 1:20 PM
Gravatar
OnBeforeUnload is IE-specific and is also limiting in that it forces a dialog with predefined text to pop up.

# re: JavaScript OnUnload event broken in IE6

Left by Ron MacCracken at 6/20/2006 8:45 AM
Gravatar
The onBeforeUnload event did not work for me because it seems to fire anytime an href is used - which in my app we use some javascript calls in my href (probably shouldn't, but we do).

I continued searching for an answer to the problem and there seems to be another IE specific way to do it.

<SCRIPT FOR=window EVENT=onunload>
alert('onunload');
</SCRIPT>

http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onunload.asp

It seems to work for me.

Ron

Comments have been closed on this topic.