Access a window by window name

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.


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.


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

Tags:

Javascript