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


Solution 1:

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.

Solution 2:

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