JS:当儿童窗口关闭时听 [英] JS: Listen when child window is closed

查看:125
本文介绍了JS:当儿童窗口关闭时听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开以这种方式分享facebook的子窗口:

I am opening child window for facebook sharing this way:

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

当用户点击共享或关闭时窗口自动关闭...有没有办法添加一个听众参加这些活动?

The window is automatically closed when user click share or close... Is there a way to add a listeners to these events?

推荐答案

如果在调用 window.open()时存储对子窗口的引用,然后您可以使用 setInterval()进行轮询,以查看窗口是否仍然使用 window.closed 财产。以下示例每秒检查两次。

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);
    }
}

注意他人:如果你曾经在你可以控制子窗口中的html的情况下,你可以使用 onbeforeunload 事件并提醒父窗口。

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.

这篇关于JS:当儿童窗口关闭时听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆