Solved

How to detect previously opened window in Javascript

Posted on 2003-12-01
9
423 Views
Last Modified: 2011-09-20
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?

0
Comment
Question by:pbrooks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
9 Comments
 
LVL 25

Assisted Solution

by:devic
devic earned 100 total points
ID: 9853646
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
 

Author Comment

by:pbrooks
ID: 9853902
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
 
LVL 25

Expert Comment

by:devic
ID: 9853961
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:pbrooks
ID: 9855057
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
 
LVL 10

Accepted Solution

by:
NetGroove earned 400 total points
ID: 9856231
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
 

Author Comment

by:pbrooks
ID: 9859818
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9859950
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
 
LVL 10

Expert Comment

by:NetGroove
ID: 9862607
Thanks C101.

Thank you pbrooks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question