如何监听子窗口关闭?

发布于 2024-11-02 08:17:43 字数 182 浏览 2 评论 0原文

我以这种方式打开 Facebook 共享的子窗口:

window.open(sharingUrl,'','toolbar=0,status=0,width=626,height=436');

当用户单击共享或关闭时,该窗口会自动关闭...

有没有办法为这些事件添加侦听器?

I am opening a child window for Facebook sharing this way:

window.open(sharingUrl,'','toolbar=0,status=0,width=626,height=436');

The window is automatically closed when a user clicks share or close...

Is there a way to add a listener to these events?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

不…忘初心 2024-11-09 08:17:43

如果您在调用 window.open() 时存储对子窗口的引用,则可以使用 setInterval() 进行轮询,以查看窗口是否仍处于打开状态window.close 属性。下面的示例每秒检查两次。

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
var timer = setInterval(checkChild, 500);

function checkChild() {
    if (child.closed) {
        alert("Child window closed");   
        clearInterval(timer);
    }
}

其他注意事项:如果您可以控制子窗口中的 html,则可以使用 onbeforeunload 事件并提醒父窗口。

If you store a reference to the child window when you call window.open(), then you can poll using setInterval() to see whether the window is still open using the window.closed property. The example below checks twice per second.

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
var timer = setInterval(checkChild, 500);

function checkChild() {
    if (child.closed) {
        alert("Child window closed");   
        clearInterval(timer);
    }
}

Note to others: If you are ever in a situation where you have control over the html in the child window, you could make use of the onbeforeunload event and alert the parent window.

没有心的人 2024-11-09 08:17:43

为了供将来参考,我想分享另一个不需要 setInterval 的解决方案:

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
child.onunload = function(){ console.log('Child window closed'); };

For future references, I'd like to share another solution that doesn't require setInterval :

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
child.onunload = function(){ console.log('Child window closed'); };
甜妞爱困 2024-11-09 08:17:43

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');

// Called even when loading and unloading.
child.onunload = function(){ 

    // What matters here
    setTimeout(function(){
        if (child.closed) {
            console.log('Child window closed'); 
        }
    }, 300);
};

var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');

// Called even when loading and unloading.
child.onunload = function(){ 

    // What matters here
    setTimeout(function(){
        if (child.closed) {
            console.log('Child window closed'); 
        }
    }, 300);
};
归途 2024-11-09 08:17:43

这会工作得很好

   <html>
<head>
<title>Detecting browser close in IE</title>
<script type="text/javascript">

window.onbeforeunload = function(){ myUnloadEvent(); }
function myUnloadEvent() {
    alert ('You can have Ur Logic Here ,');

}

</script>
</head>
</script>
</head>
<body >
<h1>Close the Window now</h1>
</body>
</html>

This will work fine

   <html>
<head>
<title>Detecting browser close in IE</title>
<script type="text/javascript">

window.onbeforeunload = function(){ myUnloadEvent(); }
function myUnloadEvent() {
    alert ('You can have Ur Logic Here ,');

}

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