Close all pop-up windows [duplicate]

旧城冷巷雨未停 提交于 2019-12-09 07:18:22

问题


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');

回答1:


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:


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()
}


来源:https://stackoverflow.com/questions/17756376/close-all-pop-up-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!