44

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

Dave
  • 8,667
  • 25
  • 72
  • 90
  • 2
    Assign the return value of `window.open()` to a variable. This is the only proper solution. – ThiefMaster Aug 30 '11 at 13:33
  • 5
    -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
  • 2
    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
  • 1
    Look at this: http://stackoverflow.com/questions/2455158/find-window-previously-opened-by-window-open – Jack Allan May 28 '13 at 14:34
  • 1
    @Dave: The accepted answer is simply incorrect, see [Petr's answer](http://stackoverflow.com/a/18542652/157247). – T.J. Crowder Jun 15 '15 at 04:58

7 Answers7

23

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

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.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • 2
    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
  • 1
    @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
  • 7
    @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 05:32
8

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.

Johannes
  • 97
  • 1
  • 2
0

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

Tom Jiang
  • 324
  • 3
  • 2
0

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
-2

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);
CatPerson
  • 137
  • 7
-7

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

HBublitz
  • 670
  • 4
  • 6