关闭所有弹出窗口 [英] Close all pop-up windows

查看:107
本文介绍了关闭所有弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题有很多答案.

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

这篇关于关闭所有弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆