/    Sign up×
Community /Pin to ProfileBookmark

How to check if a named window is open?

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:

[code=php]
//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();
}
[/code]

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!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisSep 15.2003 — 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.
Copy linkTweet thisAlerts:
@jovialjonnyauthorSep 19.2003 — [i]Originally posted by gil davis [/i]

[B]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. [/B][/QUOTE]


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

[code=php]
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");
}
[/code]
Copy linkTweet thisAlerts:
@gil_davisSep 19.2003 — [b]Is there any way to check if the window is open without having the blank window open?[/b][/quote]
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.
Copy linkTweet thisAlerts:
@WebskaterSep 19.2003 — 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.
×

Success!

Help @jovialjonny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...