关闭所有弹出窗口

我知道有很多这样的问题有很多答案。

我知道我可以使用

var popup = window.open(''); 

以后可以使用

 popup.close(); 

关闭那个窗口。

但是,有没有办法关闭所有孩子而不必存储window.open结果?

那就是我能做到的

 window.open('1'); window.open('2'); window.open('3'); 

然后以某种方式做一个关闭这三个窗口的全局“关闭”调用?

如果没有,我可以通过使用以下代码来完成它吗?

 window.open('1','window1'); window.open('2','window2'); window.open('3','window3'); 

您可以创建一个新function,基本上将现有function包装在您尝试执行的操作中。

 var WindowDialog = new function() { this.openedWindows = {}; this.open = function(instanceName) { var handle = window.open(Array.prototype.splice.call(arguments, 1)); this.openedWindows[instanceName] = handle; return handle; }; this.close = function(instanceName) { if(this.openedWindows[instanceName]) this.openedWindows[instanceName].close(); }; this.closeAll = function() { for(var dialog in this.openedWindows) this.openedWindows[dialog].close(); }; }; 

样品使用

 WindowDialog.open('windowName', /* arguments you would call in window.open() */); WindowDialog.open('anotherName', /* ... */); WindowDialog.open('uniqueWindow', /* ... */); WindowDialog.open('testingAgain', /* ... */); WindowDialog.open('finalWindow', /* ... */); // closes the instance you created with the name 'testingAgain' WindowDialog.close('testingAgain'); // close all dialogs WindowDialog.closeAll(); 

试试这个打开和关闭

 document.MyActiveWindows= new Array; function openWindow(sUrl,sName,sProps){ document.MyActiveWindows.push(window.open(sUrl,sName,sProps)) } function closeAllWindows(){ for(var i = 0;i < document.MyActiveWindows.length; i++) document.MyActiveWindows[i].close() }