在卸载回调中将 setTimeout() 附加到 window.opener
我正在尝试这样做:
$(function() {
var parent = window.opener;
$(window).bind('unload', function() {
parent.setTimeout(function() {
parent.console.log('Fired!');
}, 200);
}
});
上面的示例在 FF、Chrome 等中运行良好,但在 IE8 中不起作用。在后者中,setTimeout() 中指定的回调似乎永远不会被触发。
基本原理是,当弹出窗口关闭时,我想在父窗口(window.opener)中执行一些代码。我希望弹出窗口对此负责,而不是相反。
只是为了表明这个概念是有效的:
$(function() {
var parent = window.opener;
$(window).bind('unload', function() {
parent.console.log('Fired!');
}
});
在绑定到卸载的回调中立即调用 console.log (如上面的示例)似乎在所有浏览器中都有效(此处不针对 IE6),但是一旦我将 setTimeout() 添加到它打破的混合。
是否可以?是范围问题吗?
I'm trying to do this:
$(function() {
var parent = window.opener;
$(window).bind('unload', function() {
parent.setTimeout(function() {
parent.console.log('Fired!');
}, 200);
}
});
The example above works well in FF, Chrome etc. but not IE8. In the latter, the callback specified in setTimeout() never seems to be fired.
Rationale is that I would like to execute some code in the parent window (window.opener), when a popup window is closed. I would like the popup to be responsible for this, and not the other way around.
Just to show that the concept works:
$(function() {
var parent = window.opener;
$(window).bind('unload', function() {
parent.console.log('Fired!');
}
});
Calling console.log immediately in the callback bound to unload (as in the example above) seems to be working in all browsers (not targeting IE6 here), but as soon as I add setTimeout() to the mix it breaks.
Is it possible? Is it a scope issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 IE 中,函数绑定到其所有者窗口。它们可以从另一个窗口调用,但是当一个窗口被卸载时,它的所有功能都会消失。如果您尝试在 onunload 之后显式调用其中一个,您将收到“调用的对象已与其客户端断开连接”错误。
因此,在子进程 onunload 中,您应该立即回调父进程。如果父母需要延迟,则必须自行提供。
(您可能还应该检查父级是否为
null
,是否已关闭
,并将访问尝试包装在try
中,以便如果父窗口已关闭或导航,您不会收到错误。)In IE, functions are bound to their owner window. They can be called from another window, but when a window is unloaded, all its functions die. If you try to call one explicitly after onunload, you'll get a ‘The object invoked has disconnected from its clients’ error.
So in the child onunload you should call the parent back immediately. If the parent needs a delay, it'll have to provide it itself.
(You should probably also check that the parent is not
null
, hasn't beenclosed
, and wrap the access attempt in atry
, so that you don't get an error if the parent window has been closed or navigated.)