Javascript:如何获取所有打开的窗口列表

时间:2014-07-07 01:36:22

标签: javascript window

假设您使用以下命令打开一些窗口:

  window.open(url1,'win1');
  window.open(url2,'win2');
  window.open(url3,'win3');

(每个窗口都有一个唯一的名称)

然后刷新页面。

3个弹出窗口仍然打开。有没有办法列出所有打开的窗口的名称并关闭它们?

这不是一个重复的问题。

在这个问题中,浏览器正在刷新,所以你不能简单地使用全局数组来跟踪子窗口。

这不是一个重复的问题。

2 个答案:

答案 0 :(得分:5)

所以问题已经结束,我会根据评论和研究发表答案。

首先,对所有评论的人,谢谢你的帮助。

答案: 没有内置对象跟踪打开的窗口并且从页面加载到页面加载持续存在。

正如Felix Kling指出的那样,使用localStorage是一种可能的解决方法。

答案 1 :(得分:2)

尝试使用postMessage在同一域中的现有窗口之间进行通信。这就是我要尝试解决同样的问题。请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

  

的index.htm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>pop</title>
  </head>
  <body>

    <script>

var pops = [];

window.onmessage = function(e)
{

    // todo: check domain 
    // if( e.origin )
    var data;
    try
    {
        data = JSON.parse(e.data);
    }
    catch(e)
    {
        // fail silent...?
        return;
    }
    switch(data.event)
    {
    case "RestoreOpenWindow":
        onClosePopup(e.source.name);
    case "QueryOpenWindows":
        pops.push(e.source);
        updateLabel();
        break;
    }
};

window.onload = function()
{
    window.onClosePopup = onClosePopup;
    updateLabel();
};

window.onbeforeunload = function()
{
    for(var i = 0; i < pops.length; i++) pops[i].queryOpenPopups();
};

function onClosePopup(name)
{
    for(var i = pops.length - 1; i >= 0; i--)
        if(pops[i].name === name)
            { pops.splice(i, 1); break; }
    updateLabel();
};

function openPopup()
{
    pops.push(window.open("pop/popup.htm", "pop" + pops.length, ' '));
    updateLabel();
    setTimeout(function(){
        alert('Click ok to refresh...');
        location.href = location.href;
    }, 5000);
}

function updateLabel()
{
    document.getElementById("total").innerHTML = pops.length;
    var html = [];
    for(var i = 0; i < pops.length; i++)
        html.push(pops[i].name);
    document.getElementById("names").innerHTML = html.join("<br"+"/"+">");
}


    </script>
    <button onclick="openPopup()">open popup and refresh after 5 seconds (...allow em popups...)</button></br>
    <span>total: </span><span id="total"></span></br>
    <span id="names"></span></br>
  </body>
</html>
  

popup.htm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>pop</title>
  </head>
  <body>

    <script>

window.queryOpenPopups = function()
{
    var count = 0;
    var hInterval = setInterval(function () {
        try
        {
            if(window.opener)
            {
                window.opener.postMessage(JSON.stringify({"event": "QueryOpenWindows", "name": window.name}), "*");
                clearInterval(hInterval);
            } else count++;
        }
        catch(e)
        {
            count++;
        }       
        if(count > 50)window.close();
    }, 100);
};

window.onbeforeunload = function(){
    window.opener.onClosePopup(window.name);
};

// restore link with opener on refresh
window.opener.postMessage(JSON.stringify({"event": "RestoreOpenWindow", "name": window.name}), "*");

window.onload=function(){ document.getElementById("name").innerHTML = window.name; };
    </script>       
    <span id="name"></span>
  </body>
</html>