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

Javascript:如何获取所有打开窗口的列表,javascript,window,Javascript,Window,假设您打开了几个窗口,其中包括: window.open(url1,'win1'); window.open(url2,'win2'); window.open(url3,'win3'); (每个窗口都有一个唯一的名称) 然后刷新页面 3个弹出窗口仍然打开。有没有办法列出所有打开窗口的名称并关闭它们 这不是重复的问题。 在这个问题中,浏览器正在刷新,因此您不能简单地使用全局数组来跟踪子窗口 这不是一个重复的问题。因此问题已结束,我将根据评论和研究发布答案 首先,对于所有发表评论的人

假设您打开了几个窗口,其中包括:

  window.open(url1,'win1');
  window.open(url2,'win2');
  window.open(url3,'win3');
(每个窗口都有一个唯一的名称)

然后刷新页面

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

这不是重复的问题。

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


这不是一个重复的问题。

因此问题已结束,我将根据评论和研究发布答案

首先,对于所有发表评论的人,感谢你们的帮助

答复: 没有一个内置对象可以跟踪打开的窗口并在页面加载之间保持不变


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

尝试使用postMessage在同一域内的现有窗口之间进行通信。这就是我试图解决同样问题的方法。见:

index.htm


流行音乐
var pops=[];
window.onmessage=函数(e)
{
//todo:检查域
//如果(如原产地)
var数据;
尝试
{
data=JSON.parse(e.data);
}
捕获(e)
{
//失败沉默。。。?
返回;
}
开关(data.event)
{
案例“RestorePenWindow”:
onClosePopup(例如source.name);
案例“查询窗口”:
pops.push(e.source);
updateLabel();
打破
}
};
window.onload=函数()
{
window.onClosePopup=onClosePopup;
updateLabel();
};
window.onbeforeunload=函数()
{
对于(var i=0;i=0;i--)
if(pops[i].name==name)
{pops.splice(i,1);break;}
updateLabel();
};
函数openPopup()
{
pops.push(window.open(“pop/popup.htm”,“pop”+pops.length,”);
updateLabel();
setTimeout(函数(){
警报('单击确定刷新…');
location.href=location.href;
}, 5000);
}
函数updateLabel()
{
document.getElementById(“total”).innerHTML=pops.length;
var html=[];
对于(变量i=0;i
总数:

popup.htm


流行音乐
window.queryOpenPopups=函数()
{
var计数=0;
var hInterval=setInterval(函数(){
尝试
{
if(窗口开启器)
{
window.opener.postMessage(JSON.stringify({“事件”:“QueryOpenWindows”,“name”:window.name}),“*”;
净空间隔(腹地);
}else-count++;
}
捕获(e)
{
计数++;
}       
如果(计数>50)窗口关闭();
}, 100);
};
window.onbeforeunload=函数(){
window.opener.onClosePopup(window.name);
};
//刷新时恢复与opener的链接
window.opener.postMessage(JSON.stringify({“事件”:“RestoreOpenWindow”,“name”:window.name}),“*”;
window.onload=function(){document.getElementById(“name”).innerHTML=window.name;};

没有,除非URL来自同一个域。然后可能会在这些窗口之间进行通信,但我认为您无法以编程方式关闭它们,因为您丢失了对这些窗口的引用。URL位于同一个域中。然后,它们可以按照建议写入localStorage。也许让子窗口侦听更改并让它们自己关闭。localStorage可以工作。我希望窗口对象中可能有我尚未找到的属性。我不这么认为。当您重新加载页面时,您正在创建一个新的JS环境,它与以前的环境无关,即使它是同一个页面。
<!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>
<!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>