簡體   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