admin管理员组

文章数量:1391968

I want to bind a function to the beforeunload event of a child window in Javascript. I have the following code:

newWind = window.open(settings.url, "Dialog", "width=" + settings.width + ",height=" + settings.height + ",resizable=" + settings.resizable + ",scrollbars=" + true);

newWind.onunload = function() { alert('test'); }

Works fine in Firefox, but fails in IE7. Can anyone see what I am doing wrong?

I want to bind a function to the beforeunload event of a child window in Javascript. I have the following code:

newWind = window.open(settings.url, "Dialog", "width=" + settings.width + ",height=" + settings.height + ",resizable=" + settings.resizable + ",scrollbars=" + true);

newWind.onunload = function() { alert('test'); }

Works fine in Firefox, but fails in IE7. Can anyone see what I am doing wrong?

Share Improve this question edited Feb 26, 2020 at 7:54 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 13, 2009 at 10:48 Fiona - myaccessible.websiteFiona - myaccessible.website 14.9k17 gold badges84 silver badges118 bronze badges 1
  • based on your title, did you mean to set newWind.onbeforeunload? I also take it that defining the onbeforeunload in the "settings.url" page is not an option? – scunliffe Commented Nov 13, 2009 at 11:43
Add a ment  | 

2 Answers 2

Reset to default 4
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.

I'm using the following hack to find out when the child window was closed :

function checkChildWindow(win, onclose) {
    var w = win;
    var cb = onclose;
    var t = setTimeout(function() { checkChildWindow(w, cb); }, 500);
    var closing = false;
    try {
        if (win.closed || win.top == null) //happens when window is closed in FF/Chrome/Safari
        closing = true;        
    } catch (e) { //happens when window is closed in IE        
        closing = true;
    }
    if (closing) {
        clearTimeout(t);
        onclose();
    }
}

function openWindow(url) {  
    ...
    var wnd = window.open(url, "window_name", "[...flags...]");
    checkChildWindow(wnd, function() { alert('child was closed!'); } );
    ...
}

本文标签: javascriptwindowonbeforeunload not firing in child windowStack Overflow