如何监听子窗口关闭?
我以这种方式打开 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在调用
window.open()
时存储对子窗口的引用,则可以使用setInterval()
进行轮询,以查看窗口是否仍处于打开状态window.close
属性。下面的示例每秒检查两次。其他注意事项:如果您可以控制子窗口中的 html,则可以使用 onbeforeunload 事件并提醒父窗口。
If you store a reference to the child window when you call
window.open()
, then you can poll usingsetInterval()
to see whether the window is still open using thewindow.closed
property. The example below checks twice per second.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.
为了供将来参考,我想分享另一个不需要
setInterval
的解决方案: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');
var child = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
这会工作得很好
This will work fine