IE7 window.onunload vs. top.window.onunload bug

Hi,

I was testing some code in IE7 with framesets and got a strange bug. If you only have one window open and do the following,

window.onunload = unloadFunc;
top.window.onunload = unloadFunc;

function unloadFunc()
{
alert('Goodbye');
}

function unloadFunc2()
{
alert('Hasta la vista');
}

then unloadFunc() gets fired twice. It seems as if the window.onunload and top.window.onunload are 2 separate events. I tried to see if window == top.window and the result is true.

window.onunload = unloadFunc;
top.window.onunload = unloadFunc2;

If I do the above, both functions get executed. I get 'Hasta la vista' followed by 'Goodbye'.

Do you have any idea why there are 2 separate events? Does it have to do with the new tabs or is it just a bug in IE7?

I have attached a sample file. You'll need to rename it to an html extension to test.

Thanks in advance
[1024 byte] By [JetDeveloper] at [2007-11-20 4:16:46]
# 1 Re: IE7 window.onunload vs. top.window.onunload bug
There is no bug here.

then unloadFunc() gets fired twice. It seems as if the window.onunload and top.window.onunload are 2 separate events. I tried to see if window == top.window and the result is true.
It got fired twice because you called onunload to the same window twice. Since you are not in a frame calling back to the frameset, window = top.window.
PeejAvery at 2007-11-8 0:41:31 >
# 2 Re: IE7 window.onunload vs. top.window.onunload bug
What about the 2nd case though, where 2 different functions are called when I close the window?

window.onunload = unloadFunc;
top.window.onunload = unloadFunc2;

In IE7, both of them get called. There are 2 alerts:
'Hasta la vista', then 'Goodbye'

In IE6, only the unloadFunc gets called, so I just see
'Goodbye'

However,

window.onunload = unloadFunc;
window.onunload = unloadFunc2;

will just show 'Hasta la vista' once in both IE6 and IE7
JetDeveloper at 2007-11-8 0:42:42 >
# 3 Re: IE7 window.onunload vs. top.window.onunload bug
Maybe Microsoft is attempt to allow multiple events attached to one object. Firefox allows only one event but overwrites the previous ones.

Either way, in this case window = top.window.
PeejAvery at 2007-11-8 0:43:34 >