window.onbeforeunload not firing in child window

我的未来我决定 提交于 2019-12-13 00:26:16

问题


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?


回答1:


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.




回答2:


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!'); } );
    ...
}


来源:https://stackoverflow.com/questions/1728495/window-onbeforeunload-not-firing-in-child-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!