Javascript:如何获取所有打开窗口的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24601971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: How to get a list of all open windows
提问by Brian McGinity
Suppose you open a handful of windows with:
假设你打开了几个窗口:
window.open(url1,'win1');
window.open(url2,'win2');
window.open(url3,'win3');
(each window has a unique name)
(每个窗口都有一个唯一的名称)
And then you refresh the page.
然后刷新页面。
The 3 popup windows are still open. Is there a way to list the names of all of the open windows and close them?
3 个弹出窗口仍处于打开状态。有没有办法列出所有打开的窗口的名称并关闭它们?
This is not a duplicate question.
这不是一个重复的问题。
In this question the browser is being refreshed, so you cannot simply use a global array to keep track of child windows.
在这个问题中,浏览器正在刷新,因此您不能简单地使用全局数组来跟踪子窗口。
This is not a duplicate question.
这不是一个重复的问题。
回答by Brian McGinity
So the questions is closed, I'll post an answer based on the comments and research.
所以问题已经结束,我将根据评论和研究发布答案。
Firstly, to all who commented, thank you for helping.
首先,感谢所有评论的人,感谢您的帮助。
Answer: There is not a built-in object which tracks opened windows and persists from page load to page load.
答:没有一个内置对象可以跟踪打开的窗口并从页面加载到页面加载持续存在。
As Felix Kling pointed out, using localStorage is a possible work-around.
正如 Felix Kling 指出的那样,使用 localStorage 是一种可能的解决方法。
回答by Vozzie
Try postMessage to communicate between existing windows within the same domain. That's how i'm going to try and solve the same problem. See: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
尝试 postMessage 在同一域内的现有窗口之间进行通信。这就是我将如何尝试解决同样的问题。请参阅:https: //developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
index.htm
索引.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
弹出窗口.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>