Javascript 查找以前由 window.open 打开的窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2455158/
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
Find window previously opened by window.open
提问by spender
We've got the following situation, running from a single domain:
我们有以下情况,从单个域运行:
Page A uses window.open()to open a named window (a popup player). window.open()gives page A a reference to the window.
页面 A 用于window.open()打开一个命名窗口(弹出式播放器)。window.open()为页面 A 提供对窗口的引用。
User now reloads page A. The reference to the named window is lost. Using window.open()to "find" the window has the unfortunate side effect of reloading it (undesirable). Is there any other way to get a reference to this window?
用户现在重新加载页面 A。对命名窗口的引用丢失。使用window.open()“查找”窗口具有重新加载它的不幸副作用(不可取)。有没有其他方法可以获得对这个窗口的引用?
回答by awe
Try this:
尝试这个:
var playerUrl = 'http://my.player...';
var popupPlayer= window.open('', 'popupPlayer', 'width=150,height=100') ;
if(popupPlayer.location.href == 'about:blank' ){
popupPlayer.location = playerUrl ;
}
popupPlayer.focus();
It will open a blank window with a unique name. Since the url is blank, the content of the window will not be reloaded.
它将打开一个具有唯一名称的空白窗口。由于 url 为空,因此不会重新加载窗口的内容。
回答by Alexander Malfait
AFAIK, no there isn't..
AFAIK,不,没有..
A kind-of-dirty-but-i-guess-it-will-work hack would be to periodically reset the reference on the parent window from within the popup using window.opener, with something like this code:
一种肮脏但我猜它会起作用的技巧是使用 window.opener 从弹出窗口中定期重置父窗口上的引用,代码如下:
setInterval(function() {
if(window.opener) {
window.opener.document.myPopupWindow = window
}
}, 100)
In the parent window, you'll be able to access document.myPopupWindow, even after a reload (well, 100ms after the reload). This should work cross browser.
在父窗口中,您将能够访问 document.myPopupWindow,即使在重新加载后(嗯,重新加载后 100 毫秒)。这应该可以跨浏览器工作。
回答by OverLex
Actually what you did is destroy the parent (page A) of the created window (Popup), so it has no more reference to the original parent therefore you can't get a direct reference.
实际上,您所做的是销毁所创建窗口(弹出窗口)的父级(页面 A),因此它不再引用原始父级,因此您无法获得直接引用。
The only solution I can think of is using a browser that offers you added javascript capability to cycle through active windows (tabs) and find one that has a special property (ie: your reloaded page A) that gets recognized by the popup.
我能想到的唯一解决方案是使用一种浏览器,它为您提供添加的 javascript 功能来循环浏览活动窗口(选项卡)并找到一个具有特殊属性(即:您重新加载的页面 A)并被弹出窗口识别的浏览器。
Unfortunately I guess only firefox has some added capability or extension that gives you this flexibility. (it is also a security risk though)
不幸的是,我想只有 Firefox 有一些附加功能或扩展,才能为您提供这种灵活性。(虽然这也是一个安全风险)
回答by Johan
This should work. Add this code in the popup:
这应该有效。在弹出窗口中添加此代码:
function updateOpener() {
if (window.opener)
window.opener.document.myPopupWindow = window;
else
setTimeout(updateOpener, 100);
}
updateOpener();
And this in onload of the parent window. To make sure myPopupWindow have been set wait 100 ms before accessing it.
这在父窗口的加载中。要确保 myPopupWindow 已设置,请在访问它之前等待 100 毫秒。
setTimeout(function() {
if (document.myPopupWindow)
document.myPopupWindow.focus();
}, 100);
回答by Gleba
If all the windows share a common Url origin you can register a ServiceWorker and then access all windows from the ServiceWorker: https://developer.mozilla.org/en-US/docs/Web/API/Clients
如果所有窗口共享一个公共 URL 源,您可以注册一个 ServiceWorker,然后从 ServiceWorker 访问所有窗口:https: //developer.mozilla.org/en-US/docs/Web/API/Clients
AFAIK You won't be able to pass a reference to other windows from WorkerService to your window but you can establish communications with the ServiceWorker via
AFAIK 您将无法将 WorkerService 对其他窗口的引用传递到您的窗口,但您可以通过以下方式与 ServiceWorker 建立通信

