[英]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.