javascript 关闭所有弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17756376/
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
Close all pop-up windows
提问by David
I know there are many questions of this ilk with many answers.
我知道有很多类似的问题有很多答案。
I know that I can use
我知道我可以使用
var popup = window.open('');
and can later use
并且以后可以使用
popup.close();
to close that window.
关闭那个窗口。
However, is there a way to close all children without having to store the window.open result?
但是,有没有办法关闭所有子项而不必存储 window.open 结果?
That is, could I do
也就是说,我能不能做
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');
回答by Austin Brunkhorst
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();
};
};
Sample Usage
示例用法
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();
回答by Daniel Ezra
try this to open and close
试试这个来打开和关闭
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()
}