如何收听子窗口关闭? [英] How to listen for child window closing?
问题描述
我正在以这种方式打开Facebook子窗口:
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?
推荐答案
如果在调用 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,则可以使用
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.
这篇关于如何收听子窗口关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!