Javascript 通过窗口名称访问窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7243970/
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
Access a window by window name
提问by Dave
If I open a window using
如果我打开一个窗口使用
window.open('myurl.html', 'windowname', 'width=100,height=100');
How do I refer to the new window (from the same page that opened it) using 'windowname'? This question is specifically about this. I'm aware that I could save a reference to the handle by using "var mywin = window.open(...)" but I don't care about that in this situation.
如何使用“windowname”引用新窗口(从打开它的同一页面)?这个问题是专门针对这个的。我知道我可以通过使用“var mywin = window.open(...)”来保存对句柄的引用,但在这种情况下我不在乎。
Thanks, - Dave
谢谢, - 戴夫
采纳答案by HBublitz
afaik there's no way like windows['windowname']
.
The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >
afaik 没有办法像windows['windowname']
. 在 window.open() 中分配的“windowname”可以作为目标寻址<a target="windowname" [...] >
回答by Wladimir Palant
If you didn't save a reference to the window then there is no way to restore it. However, ifthat window is still open and ifthe page loaded there belongs to the same domain as your page, you can run JavaScript code in it:
如果您没有保存对窗口的引用,则无法恢复它。但是,如果该窗口仍处于打开状态并且如果页面加载有属于相同的域名页面,您可以运行在它的JavaScript代码:
window.open("javascript:doSomething()", "windowname");
Whether that's sufficient in your scenario depends on what you are trying to achieve.
在您的场景中这是否足够取决于您要实现的目标。
回答by Petr ''Bubák'' ?edivy
In firefox (might work in other browsers too, but now it's not my concern) I was able to reference one window accross multiple page loads with
在 firefox 中(也可能在其他浏览器中工作,但现在不是我关心的问题)我能够在多个页面加载中引用一个窗口
var w = window.open("", "nameofwindow");
This opens new window if it doesn't exist and return reference to existing window if it does exist without changing contents of the window.
如果它不存在,则打开新窗口,如果存在,则返回对现有窗口的引用,而不更改窗口的内容。
With jQuery I was then able to append new content, to make quick collection of interresting links like this
使用 jQuery,我可以添加新内容,以快速收集这样的相关链接
$('body', w.document).append(link_tag);
回答by Johannes
Petr is correct:
彼得是正确的:
var w = window.open("", "nameofwindow");
works in all browsers, I am using it to retrieve the reference to the window object previously opened by a different page. The only problem is the initial opening of the page, if the popup does not exist, you will get a new window with a blank page.
适用于所有浏览器,我使用它来检索对先前由不同页面打开的窗口对象的引用。唯一的问题是页面的初始打开,如果弹出窗口不存在,您将获得一个带有空白页面的新窗口。
I tried invoking a Javascript function inside the context of the other document in order to check whether I opened a new window or retrieved the already active page. If the check fails, I just invoke window.open again to actually load my popup content:
我尝试在另一个文档的上下文中调用 Javascript 函数,以检查我是否打开了新窗口或检索了已激活的页面。如果检查失败,我只需再次调用 window.open 来实际加载我的弹出内容:
var w = window.open("http://mydomain.com/myPopup", "nameofwindow");
Hope that helps.
希望有帮助。
回答by Tom Jiang
Try open that window with the name, but URL is '' again, to check if it's a blank window or not. If it's in open, then you will get the window; if not, a new window open, and you need close it. Add the children in localStorage will help to prevent to open the new blank window.
尝试使用名称打开该窗口,但 URL 再次为 '',以检查它是否为空白窗口。如果它是打开的,那么你会得到窗户;如果没有,则会打开一个新窗口,您需要关闭它。在 localStorage 中添加子项将有助于防止打开新的空白窗口。
Please check my code in https://github.com/goldentom66/ParentChildWindow
回答by CatPerson
Sorry I am posting late, but if you still have the other window open, and they are on the same domain, you can run, on the first window:
抱歉,我发布晚了,但是如果您仍然打开另一个窗口,并且它们在同一个域中,您可以在第一个窗口上运行:
function getReference(w) {
console.log('Hello from', w);
}
And on the second window:
在第二个窗口上:
window.opener.getReference(window);
回答by ThiefMaster
It is not possible. The windowName is just to be used in target="..."
of links/forms or to use the same name again in another window.open
call to open a new url in that window.
这不可能。windowName 只是在target="..."
链接/表单中使用,或者在另一个window.open
调用中再次使用相同的名称以在该窗口中打开一个新的 url。