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 "window.open('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?

pbrooksAsked:
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.

devicCommented:
hi pbrooks,

here is an example:
=================================
<html>
<head>
    <title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--// created by devic :oldlook.experts-exchange.com
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+" = window.open(url, myname, winprops)");
if (parseInt(navigator.appVersion) >= 4) { eval(myname+".window.focus()"); }
}

function checkWindow(namez)
{
      try{
            if(eval("window."+namez+".document"))
            {
                  alert("Window \""+namez+"\" open");
            }
            else
            {
                  alert("Window \""+namez+"\" not found");
            }
      }
      catch(e)
      {
            alert("Window \""+namez+"\" not found");
      }
}
//-->
</SCRIPT>
   
</head>

<body>

<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');" />
</form>
</body>
</html>
0
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).

Example:

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

0
devicCommented:
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.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0
NetGrooveCommented:
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 window.open() 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 = window.open('', '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 = window.open('', 'sidebar');
 if(nw.myGlobalVar=='undefined'){
    nw = window.open('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 = window.open('', 'sidebar');
 if(nw.myGlobalVar=='undefined'){
    nw = window.open('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?


0

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:
NetGroove,
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 window.open('', '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 = window.open('', '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...

wBar.close();
window.open('bar.html','sidebar','left=400,top=0,scrollbars=yes');
}

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

}


0
NetGrooveCommented:
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:
http://www.experts-exchange.com/Community_Support/

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,
NetGroove

0
NetGrooveCommented:
Thanks C101.

Thank you pbrooks.
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.