This question already has an answer here:
这个问题已经有了答案:
- Get list of opened popup windows 2 answers
- 获取打开的弹出窗口2答案列表
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?
但是,有没有一种方法可以在不需要存储窗口的情况下关闭所有的孩子。开放的结果吗?
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');
2 个解决方案
#1
6
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();
#2
2
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()
}
#1
6
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();
#2
2
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()
}