Click to See Complete Forum and Search --> : How to check if a named window is open?


jovialjonny
09-15-2003, 11:33 AM
Hi,
I have a form that is being targetted to an iframe in another window. Right now I create a reference to that window when I open it and access various javascript methods through the reference like so:


//THE VIEWER PAGE.
var viewer = null;

function openViewer()
{ if(viewer==null || viewer.closed==true)
{ viewer = window.open("multiviewer.html","mv");
}
else
{ viewerLoaded();
}
}
function viewerLoaded()
{ var frameName = viewer.createTab();//createTab is a js method on viewer page that returns iframe name for targettting.
var servletForm = document.getElementById("svForm");
servletForm.target = frameName;
servletForm.submit();
}


This works fine but I am trying to expand this system so that any page submitting the form can check to see if 'multiviewer.html' is open and reference its scripting methods. The purpose is that I only want one of these files open at any one time, not one per opening page as it is now.

I have given the window the name "mv" but I am unsure how I can use this reference from other pages. Can anyone tell me if/how I can check from another window if the 'multiviewer.html' page is open somewhere already and if so, what is the syntax for referencing its javascript methods?

Any suggestions appreciated thanks!

gil davis
09-15-2003, 01:09 PM
If you want to access the window "mv" from a page that did not originally open it, you use:
someVar = window.open("", "mv");
By not specifying a URL, the window "mv" is "opened" without changing its location. However, if "mv" has been closed, it will open a new empty window.

jovialjonny
09-19-2003, 11:38 AM
Originally posted by gil davis
If you want to access the window "mv" from a page that did not originally open it, you use:
someVar = window.open("", "mv");
By not specifying a URL, the window "mv" is "opened" without changing its location. However, if "mv" has been closed, it will open a new empty window.

Okay that works if the "mv" page is still open and I am accessing a method in "mv"s script to check that it is still the right page--I do this in a try, catch to avoid any access errors.

The only problem remaining is if the "mv" window has been closed this code opens a new, blank window as you said.

Is there any way to check if the window is open without having the blank window open?

Right now the code is like this


try
{ viewer = window.open("","mv");
/*'checkpage' is a method in "mv" page, it
ensures this is still the correct page and goes into
the catch block if it is not*/
viewer.checkPage();
}
catch(error)
{ viewer = window.open("multiviewer.html","mv");
}

gil davis
09-19-2003, 01:13 PM
Is there any way to check if the window is open without having the blank window open?
Not really. However, after you establish the handle, you can check if the window has the right file in it using location.href. Then if it isn't the expected file, change the location.href to the right file.

Webskater
09-19-2003, 03:10 PM
Not sure if this is what you are after.
I open a window using window.open('url',qfcowindow, etc)

I test if this window exists before opening another window by using

if (qfcowindow && qfcowindow.open && !qfcowindow.closed)

If the window is already open (hidden behind other windows or minimised) it gets the focus with my new page in it - so multiple copies of the same window cannot be opened. It seems to work well.