关闭所有弹出窗口
问题描述:
我知道这个问题有很多答案.
I know there are many questions of this ilk with many answers.
我知道我可以使用
var popup = window.open('');
,以后可以使用
popup.close();
关闭该窗口.
但是,有没有一种方法可以关闭所有子项而不必存储window.open结果?
However, is there a way to close all children without having to store the window.open result?
就是我可以
window.open('1');
window.open('2');
window.open('3');
然后以某种方式进行全局关闭"调用以关闭这三个窗口?
and then somehow do a global "Close" call that will close these three windows?
如果没有,我可以使用以下代码完成打开吗?
If not, could I accomplish it by using the following code to do the open?
window.open('1','window1');
window.open('2','window2');
window.open('3','window3');
答
您可以创建一个新功能,该功能基本上将现有功能与您要执行的操作包装在一起.
You can make a new function that basically wraps the existing functionality with what you're trying to do.
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();