View Full Version : Assigning event handling functions for newly opened window

08-03-2002, 07:00 PM

I've been experiencing strange results from a function that's supposed to open a new window and assign some eventhandling functions to it:

function openTraceWindow() {
var traceWin = window.open(


// try to assign unload event handler
traceWin.onunload = function() {
alert("I've been unloaded");

// try to assign resize event handler
traceWin.onresize = function() {
alert("I've been resized");

// try to assign blur event handler
traceWin.onblur = function() {
alert("I've been blurred");

window.onload = openTraceWindow;

The contents of each anonymous function is just an arbitrary piece of code to alert me if the event takes place, i.e. if the event handling has been successfully assigned.
formTest.html contains just an ordinary HTML page with no JavaScript inside.

What I get is the following:
In IE5.5, nothing happens at all. The window is opened correctly with the page displayed correctly. Only that no alert takes place whether I resize, blur or unload/close the window.

In Mozilla 1.0, things are even more strange. After opening the new window, the onresize event is triggered -> I see "I've been resized" in the alert dialogue. After clicking ok, another alert with "I've been blurred" appears. So that event has also been triggered. This alert appears twice, so when I click ok on the first, it still appears. Then the "I've been unloaded" appears, and after clicking OK the alert dialogue disappears, but no further blurring/resizing/unloading can fire the events and the registered event handling functions.

Basically I just want to assign these eventhandling functions from the opening window. I know it can be worked around by doing all this event stuff directly in the opening window, but since I plan to construct its contents entirely through JS, I wanted to dismiss this approach.

Anybody know what's going in here and can provide some advice to this? Thanks.

08-03-2002, 09:39 PM
try to assign the events to the body..

traceWin.document.body.onunload = function() {
alert("I've been unloaded");

this should work..

08-04-2002, 04:10 AM
Thanks for your answer, unfortuanetely things aren't improving: IE5.5 comes up with an error message: "document.body is null or not an object", whereas Mozilla throws no error, but doesn't seem to assign an eventhandler properly, since I can close the window or load other content into it without firing the alert...

The IE5.5 case seems to be connected to the loading state of the child window's document - since it's not loaded completely, it can't have something assigned to it. At least that's my current explanation.