How to detect previously opened window in Javascript

context: IE only (5+):

On one of my web pages (say, "foo.html") , I open a new side window using "'bar.html', 'sidebar')".  The user may then leave foo.html.  However, when she returns to foo.html, I want to open the side window again but only if it is or has been closed.  If it's still open, then I do not want the sidebar window touched because it may have been changed by the user.

Is there a way to detect whether this sidebar window is open or not?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hi pbrooks,

here is an example:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--// created by devic
function popUp(url, myname, w, h, scroll) {
     var winl = (screen.width - w) / 2;
var wint = ((screen.height - h) / 2)-20;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=0, status=0, resizable=0,menubar=0'
eval(myname+" =, myname, winprops)");
if (parseInt(navigator.appVersion) >= 4) { eval(myname+".window.focus()"); }

function checkWindow(namez)
                  alert("Window \""+namez+"\" open");
                  alert("Window \""+namez+"\" not found");
            alert("Window \""+namez+"\" not found");


<a href="somePage.htm" onClick="popUp('','test1','295','300','no');return false;">Test1</a>
<br />
<a href="somePage.htm" onClick="popUp('','test2','295','300','no');return false;">Test2</a>
<p />
<input type="submit" value="test window test1" onClick="checkWindow('test1');" />
<input type="submit" value="test window test2" onClick="checkWindow('test2');" />
pbrooksAuthor Commented:
Thanks, devic, but unfortunately this does not work for me.  Your "checkWindow()" only works if I stay on your page, but not if I leave your page and then return to it (using, say, the browser's Back button or hyperlink).


Assume that you added another link on your page above, taking the user to a new page called "b.html".  If the user:
1. clicks on your popup('','test1') to create the "test1" side window
2. clicks on checkWindow('test1') -- this works.
3. leave this page, going to "b.html" via the new link
4. returns to this page using the Back button
5. now, checkWindow('test1') does not work

ok, then i think better to use cookies.

on your window popup onunload save cookie popup=closed;

and in every page you can ask for this cookie.

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

pbrooksAuthor Commented:
While cookies are an interesting solution, they will not work in my case.  My popup window is a login page to an entire site of hundreds of pages, and I cannot modify all of them to deal with the cookie.
Here is my proposal.

You have to know that all windows can be accessed by window name.
The window name is the second parameter in method, in your case is the concerened window name: 'sidebar'

Ok, now, opening a new window with the same name will NOT open a new window, it will reuse the already opened window.

You can write it like this:
 nw ='', 'sidebar');

Now you have to know is this your old window or is it a new window.
The new window would be a blank window withouth any code and without private variables.
So you could check for the old variables which are present in 'bar.html'.
If the regualr variables from 'bar.html' are not there, then this is an empty window.
Check it like this:

 nw ='', 'sidebar');
    nw ='bar.html', 'sidebar');

If there is no global var which you could reuse in the uper test, then you could assign a new globaö var specialy for that test.
Like this:

 nw ='', 'sidebar');
    nw ='bar.html', 'sidebar');
    nw.myGlobalVar = 'myData';

The first time when you open your bar.html you add a var of your choice to that window and check at reopen time for the existance of that var.

Do you need a complette example for this or is the explanation clear enough?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pbrooksAuthor Commented:
The problem with creating a variable in the side window is that the user may use the side window to navigate to lots of other pages inside the web site, and I expect that the variable will be lost when she activates a hyperlink.

However, I have discovered something similar: you can try to get a property of the side window after opening a blank document (as per your suggestion of'', 'sidebar')).  If you fail to read the property, then a window by that name is already open, but if you succeed, then you've successfully opened a blank window and can close it and open the real one...

function OpenSidebarIfNotAlreadyOpen() {
var wBar, sleft;

// try to open a blank window with the name: 'sidebar'

wBar ='', 'sidebar');

// now try to read a property (let's say, 'screenLeft')

try {
sleft = wBar.screenLeft;

// if we succeeded then we opened a blank window
//    and should close it and open the real one...


catch(e) { // if we failed to read the screenLeft property, then sidebar is already
// open and we don't need t odo anything


Thanks for sharing your experience with us.

But please take note that grading an answer with a B is unnecessary waste of expert points.
It is a win for you and for experts if you grade the comments with an A.
You can correct even after grading your last grading, if you want.
Post simply a grade correction request in this topic area:

And give B only if you are disappointed after several requests for clarification, never give a C, better let delete a question before giving a C.

Thanks in advance,

Thanks C101.

Thank you pbrooks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.