Access a window by window name

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.open(...)" but I don't care about that in this situation.



Thanks, - Dave





Assign the return value of window.open() to a variable. This is the only proper solution.
– ThiefMaster
Aug 30 '11 at 13:33


window.open()





-1 for not reading Daves question. var mywin=window.open() is exactly, what Dave doesn't care about.
– HBublitz
Aug 30 '11 at 13:39


var mywin=window.open()





I am not sure I understand the question... You are saying you want a way to reference a window that you open from the opener, but then saying you don't care about how to do it?
– jbabey
Aug 31 '11 at 19:47





Look at this: stackoverflow.com/questions/2455158/…
– Jack Allan
May 28 '13 at 14:34





@Dave: The accepted answer is simply incorrect, see Petr's answer.
– T.J. Crowder
Jun 15 '15 at 4:58




6 Answers
6



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


windows['windowname']


<a target="windowname" [...] >





This is simply incorrect.
– T.J. Crowder
Jun 15 '15 at 4:57





This shouldn't be the answer. See Jack Allan's link in the question's comments.
– Blue Eyed Behemoth
Aug 8 '16 at 18:41



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.





Ugh. Even if that works.. i hope nobody really uses it. But I wonder if something like window.open("javascript:void(window.opener.windowname = window);", "windowname") would work... that would do exactly what the guy asked for (not than anyone should actually use such a hack)
– ThiefMaster
Aug 30 '11 at 14:39


window.open("javascript:void(window.opener.windowname = window);", "windowname")





@ThiefMaster, I thought this was a rather elegant solution (assuming that it actually works). What is your objection to it, exactly?
– DOK
Aug 30 '11 at 16:40





It's a hack for a self-made problem - at least I can't imagine a reason why it would be impossible to store the window reference. You can always do something like wins['foo'] = window.open('...', 'foo'); giving you access to a name-to-window-object mapping througt the global var wins`
– ThiefMaster
Aug 30 '11 at 21:26


wins['foo'] = window.open('...', 'foo');


var





@ThiefMaster: Usually the problem is that the opening window navigates to a different page so keeping the reference is impossible. Still, I agree - this isn't a great solution.
– Wladimir Palant
Aug 31 '11 at 5:32



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



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.





Related answer, stackoverflow.com/a/14636251/23118.
– hlovdal
Oct 15 '14 at 15:45





Doesn't work in Chrome: bugs.chromium.org/p/chromium/issues/detail?id=50513
– michael.orchard
Apr 12 '16 at 7:44





Correction to above - it only works if the calling window opened the window you're trying to access, which is part of the question. Sorry about that, you are correct.
– michael.orchard
Apr 12 '16 at 8:02




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.


target="..."


window.open





and you gave exact same solution
– Mihai Iorga
Aug 30 '11 at 13:37



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS