English 中文(简体)
window.onbeforeunload not firing in child window
原标题:

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?

最佳回答
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! ); } );
    ...
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签