父窗口关闭时关闭所有子窗口 [英] Close all child windows when parent window is closed
问题描述
我有一个打开多个窗口的Web应用程序。我遇到的问题是,当父窗口关闭/刷新时,子窗口仍然打开。
我尝试过使用 onunload
和 onbeforeunload
但是没有人抓住窗口关闭事件(在Chrome中)和Firefox)。
我有一个窗口数组但刷新后对它们的引用会丢失。
I have a web application that opens multiple windows. The problem that I have is that when the parent window is closed/refreshed, the child windows remain opened.
I've tried using onunload
and onbeforeunload
but none of them catches the window close event (in Chrome and Firefox).
I have an array of windows but after refresh the reference to them is lost.
还有其他方法可以捕获此事件吗?
Is there any other way to catch this event?
这是我关闭窗口的代码(在卸载
closeAll() >和 onbeforeunload
关闭我打开的所有窗口,但不刷新页面时:)
This is my code related to closing windows (running closeAll()
outside unload
and onbeforeunload
closes all my opened window, but not when the page is refreshed):
window.unload = function() {
closeAll();
}
window.onbeforeunload = function() {
closeAll();
}
var closePopup = function(popup) {
removePopup(popup);
popup.close();
};
var closeAll = function() {
for (var popup in _this.popups) {
closePopup(_this.popups[popup]);
}
}
这仅适用于Chrome,但不适用于Firefox和IE (最新版本)。
This works only in Chrome but not in Firefox and IE (latest versions).
推荐答案
找到适用于最新版Chrome,Firefox和IE的正确解决方案(jQuery)。
Found a proper solution (jQuery) that works in the lastest versions of Chrome, Firefox and IE.
最初想要使用jQuery $()。unload()
function( http://api.jquery.com/unload/ )但它自1.8以来已被弃用( http://bugs.jquery.com/ticket/11733 )。由于 $()。unload()
是 $()的快捷方式.bind('unload',fn)
,我试图使用基本的,它的工作原理。
Initially wanted to use jQuery $().unload()
function (http://api.jquery.com/unload/) but it's deprecated since 1.8 (http://bugs.jquery.com/ticket/11733). Since $().unload()
is the shortcut for $().bind('unload', fn)
, I tried to use the basic one and it worked.
$(window).on('unload', function() {
closeAll();
});
这篇关于父窗口关闭时关闭所有子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!