Javascript 父窗口关闭时关闭所有子窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26324954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:43:44  来源:igfitidea点击:

Close all child windows when parent window is closed

javascript

提问by flaviu

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 onunloadand onbeforeunloadbut 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.

我有一个可以打开多个窗口的 Web 应用程序。我遇到的问题是,当父窗口关闭/刷新时,子窗口保持打开状态。我试过使用onunloadonbeforeunload但没有一个捕获窗口关闭事件(在 Chrome 和 Firefox 中)。我有一组窗口,但刷新后对它们的引用丢失了。

Is there any other way to catch this event?

有没有其他方法可以捕获此事件?

This is my code related to closing windows (running closeAll()outside unloadand onbeforeunloadcloses all my opened window, but not when the page is refreshed):

这是我与关闭窗口相关的代码(在closeAll()外面运行unloadonbeforeunload关闭所有打开的窗口,但不是在刷新页面时):

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

This works only in Chrome but not in Firefox and IE (latest versions).

这仅适用于 Chrome,但不适用于 Firefox 和 IE(最新版本)。

采纳答案by flaviu

Found a proper solution (jQuery) that works in the lastest versions of Chrome, Firefox and IE.

找到了适用于最新版本的 Chrome、Firefox 和 IE 的正确解决方案 (jQuery)。

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.

最初想使用 jQuery$().unload()函数 ( http://api.jquery.com/unload/) 但它自 1.8 ( http://bugs.jquery.com/ticket/11733)以来已被弃用。因为$().unload()是 的快捷方式$().bind('unload', fn),所以我尝试使用基本的方法并且它奏效了。

$(window).on('unload', function() {
    closeAll();
});

回答by Harutyun Abgaryan

use this

用这个

var popup = window.open("popup.html", "popup", "width=200,height=200");

window.onunload = function() {
    if (popup && !popup.closed) {
        popup.close();
    }
};

回答by Stephen Flynn

If you open all child windows with window.open, include this javascript in all pages, then CloseAll(false);from any included page will close all child, grandchildren, great-grand... etc., and redirect the first (root) page to login.aspx, and will not interfere with any event triggers because it pases the intial handler through.

如果您使用 window.open 打开所有子窗口,请在所有页面中包含此 javascript,然后CloseAll(false); 从任何包含的页面将关闭所有子页面、孙页面、曾祖页面等,并将第一个(根)页面重定向到 login.aspx,并且不会干扰任何事件触发器,因为它会传递初始处理程序。

function CloseAll(bTopFound)
{
    if (!bTopFound && nParent != null && !nParent.closed) {
        //parent window was not closed
        nParent.CloseAll(false);
        return;
    } else {
        if (nParent == null || nParent.closed)
        {
            top.location = '/login.aspx';
        }
    }

    for (var i = 0; i < Windows.length; i++)
    {
        if (!Windows[i].closed) {
            Windows[i].CloseAll(true);
        }
    }
    nParent = null;
    setTimeout(window.close, 150);
}

var Windows = [];
//override window.open to inject store child window objects
window.open = function (open) {
    return function (url, name, features) {
        // set name if missing here
        name = name || "default_window_name";
        var newWindow = open.call(window, url, name, features);
        Windows.push(newWindow);
        return newWindow;
    };
}(window.open);

var nParent = null;
window.onload = function (load) {
    return function (e) {
        nParent = window.opener;
        if (load != null) {
            load.call(e);
        }
    }
}(window.onload);

window.onunload = function (unload) {
    return function (e) {
        //promote first branch page to parent
        if (nParent != null && !nParent.closed && Windows.length > 0) {
            nParent.Windows.push(Windows[0]);
            Windows[0].nParent = nParent;
        }
        //make first child window new root
        for (var i = 1; i < Windows.length; i++) {
            Windows[i].nParent = Windows[0];
            Windows[0].Windows.push(Windows[i]);
        }
        if (unload != null) {
            unload.call(e);
        }
    }
}(window.onunload);

回答by William Trout

If the goal is to close the child window when the parent is closed or refreshed, rather than actually detecting the event, you can use a setInterval to detect when the window.opener is no longer there.

如果目标是在父窗口关闭或刷新时关闭子窗口,而不是实际检测事件,则可以使用 setInterval 来检测 window.opener 何时不再存在。

            function CloseOnParentClose() {
                if (typeof window.opener != 'undefined' && window.opener != null) {
                    if (window.opener.closed) {
                        window.close();
                    }
                }
                else {
                    window.close();
                }
            }
            $(window).ready(function () {
                setInterval(CloseOnParentClose, 1000);
            });