简体   繁体   English

如何在使用javascript卸载父窗口时关闭浏览器中的所有子窗口

[英]How to close all children windows in browser on unload of parent window with javascript

I need to find and close all children windows (whose that this page is their window.opener) on unload of the page with javascript. 我需要在使用javascript卸载页面后找到并关闭所有子窗口(该页面是他们的window.opener)。 When parent window is about to be unloaded I need to run through all opened windows and close only those that the unloaded page is their window.opener. 当父窗口即将被卸载时,我需要遍历所有打开的窗口并仅关闭那些已卸载页面为其window.opener的窗口。 What is the correct way to do so ? 正确的方法是什么? I need a cross-browser solution . 我需要一个跨浏览器的解决方案。

Create a variable for every new window you need to open, then close them like this: 为您需要打开的每个新窗口创建一个变量,然后像这样关闭它们:

var.close();

Full example: 完整示例:

var window1; 
window1 = window.open("www.google.com");
window1.close(); 

Try making use of onunload event which is responsible for closing all the pop-ups. 尝试利用负责关闭所有弹出窗口的onunload事件。

<body onunload="destroyPopUps()">

In the code above the onunload event is tied to the body element of the page. 在上面的代码中,onunload事件与页面的body元素相关联。 Here is the implementation of the destroyPopUps function. 这是destroyPopUps函数的实现。

function destroyPopUps() 
{
    if(popups.length == 0) return; 
    for(i=0; i<popups.length; i++) 
    {
        popups[i].close(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个窗口关闭所有子窗口的Windows浏览器 - Close all children windows browser from another window 如何关闭所有打开的窗口(父除外)从javascript中的父窗口中单击? - how to close all the opened windows(except the parent)onclick from a parent window in javascript? Javascript窗口关闭事件而不是所有浏览器的卸载事件 - Javascript window close event rather than unload event for all browsers 如何使用JavaScript关闭浏览器中的窗口 - How to close window in browser with javascript 如何在卸载主窗口时关闭从主窗口的iframe打开的窗口 - How to close windows that was opened from iframe of main window on unload of main window 如何在卸载之前关闭页面的所有通知(不使用Window#onunload)? - How can I close all notifications of a page before unload it (without use Window#onunload)? 如何通过在javascript中的子窗口上按下按钮来关闭父窗口 - how to close a parent window by pressing a button on child window in javascript 如何在JavaScript的子窗口关闭时调用父窗口函数? - How to call parent window function on close of child window in javascript? 在Javascript / AngularJS中检测/阻止浏览器关闭或会话存储卸载 - Detect/Prevent Browser Close or Session Storage Unload in Javascript/AngularJS 如何关闭父窗口? - how to close a parent window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM