开发者

Access a window by window name

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.ope开发者_StackOverflown(...)" but I don't care about that in this situation.

Thanks, - Dave


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

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

$('body', w.document).append(link_tag);


If you didn't save a reference to the window then there is no way to restore it. However, if that window is still open and if the page loaded there belongs to the same domain as your page, you can run JavaScript code in it:

window.open("javascript:doSomething()", "windowname");

Whether that's sufficient in your scenario depends on what you are trying to achieve.


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:

var w = window.open("http://mydomain.com/myPopup", "nameofwindow");

Hope that helps.


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.


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.

Please check my code in https://github.com/goldentom66/ParentChildWindow


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);


afaik there's no way like windows['windowname']. The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜