newWind.onunload = function() { alert( test ); }
IE won t let you assign a function from one window context to another window s on[before]unload, for some reason. You can assign a foreign function to other event handlers, including newWind.document.body.onunload
, but it s not a good idea.
Firstly because IE s garbage collection is window-based so if you close or navigate the opener window, the pop-up will try to execute a dead function, or function with dead variables in scope, resulting in weird JavaScript errors.
And secondly because you don t know from the opener when the popup document is loaded enough to be able to set the handler. At the point just after the open
call, the popup document may not have started loading at all, in which case you can t safely set any event handlers on it. So you would need a short delay before setting the onunload handler, and the user might close the window in that time. And the opener doesn t know how long to delay; instead, the child dialog would have to call the parent back to tell it it s ready. Better to have the child pop-up take charge of receiving its own events and calling the opener to let it know about them.
Cross-window scripting has endless pitfalls. Try to avoid it wherever possible. For dialogs always use in-page elements, which are much more reliable and less annoying than window popups.