在卸载回调中将 setTimeout() 附加到 window.opener

发布于 2024-09-14 00:52:57 字数 698 浏览 8 评论 0原文

我正在尝试这样做:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2024-09-21 00:52:57

在 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 been closed, and wrap the access attempt in a try, so that you don't get an error if the parent window has been closed or navigated.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文