Entry
Is there a browser wide windows array?
Are windows globally accessible?
How to check whether a named window already exists?
Sep 17th, 2004 11:42
Daniel LaLiberte, J Carty, Martin Honnen,
Client side JavaScript distinguishes windows by their name, so
window.open ('url', 'windowName')
opens a window with its name property (window.name) set to 'windowName'.
You can also set the name for an already open window which is useful
for windows not opened by JavaScript which need to be targetted by a
link or form. E.g. window.name = 'myWindowName'
There is, however, no browser wide/global array of all existing windows
(possibly for security reasons I think). Thus you can't check whether a
window with a certain name already exists. The closest thing you can
do is call open with an empty url and then test what you get.
var win = window.open ('', 'windowName')
If the window with that name already exists, win will contain the
window which you can then look inside and operate on, if access is
permitted. E.g. if (win.myStuff) alert ('must be my window').
Otherwise, if the window doesn't exist, window.open will create a new
window.
---------------------------------------------------------------------
What you could also do, if you can avoid refreshing your window, is
create an array and put each of the windows you create into the array
and reference them like that.
Example:
var windowsArray = new Array();
windowsArray[] = window.open(url, title, windowfeatures);
That should work fine. I've used it in an instant messaging client to
close all the windows.
Example of closing all the windows:
for(i=0;i<windowsArray.length;i++)
{
if((windowsArray[i]) && (!windowsArray[i].closed)
{
windowsArray[i].close();
}
}