繁体   English   中英

javascript从另一个窗口关闭一个窗口会覆盖调用窗口的名称

[英]javascript closing a window from another window overwrites calling window name

我有一个网站,打开了几个新标签页/窗口。 在网站的主页上,有一个“全部关闭”按钮,应关闭从网站打开的所有窗口。 乍一看,它似乎工作正常。 但是,按“关闭全部”链接时打开的最后一页将覆盖主页的名称。 这样的结果是,每当再次调用最后打开的页面时,它都不会在新选项卡中打开,而是在主页面选项卡中打开(这显然不是我想要的)。

看到这个mwe:

popupmanager.js:

function PopupManager() {
    this.name = "_popupmanager_";
    this.windows = {};
};

PopupManager.prototype.open = function(url, name) {
    this.windows[name] = window.open(url, name);
    this.windows[name].focus();
};

PopupManager.prototype.closeAll = function() {
    for (name in this.windows) {
        this.closeWindow(name);
    }
};

PopupManager.prototype.closeWindow = function(name) {
    if (this.windows[name]) {
        this.windows[name].close();
        delete this.windows[name];
    }
};

主页为index.html:

<!doctype html>

<html>
<head>
    <script src="./popupmanager.js"></script>
    <script>
        var popupManager = new PopupManager();
    </script>
</head>
<body>
    <ul>
        <li><a href="javascript:popupManager.open('http://www.facebook.be', 'facebook')">facebook</a></li>
        <li><a href="javascript:popupManager.open('http://www.google.be', 'google')">google</a></li>
        <li><a href="javascript:popupManager.closeAll()">close all/a></li>
    </ul>
</body>
</html>

现在,当您单击第一个链接时,将比第二个打开两个页面。 如果按“全部关闭”,一切似乎都可以正常工作。 但是,此时,主页选项卡的名称为“ google”(由于执行了函数“ closeAll”)。 如果再次单击第二个链接,它将在主页中打开,而不是在新选项卡中打开。 我在Firefox 21.0,Google Chrome 24.0.1312.60m和IE9中看到了这种行为,所以我想这是javascript的预期行为。

我只是不明白为什么要覆盖主页的名称和/或如何使脚本不覆盖主页标签的名称。 任何人?

好吧,我设法自己解决了。 我一直在对“ closeWindow”功能进行一些微调,这达到了目的:

PopupManager.prototype.closeWindow = function(name) {
    if (this.windows[name]) {
        if (!this.windows[name].closed) {
            this.windows[name].opener.name="indexpage";
            this.windows[name].close();
        }
        delete this.windows[name];
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM