Solved

How to detect previously opened window in Javascript

Posted on 2003-12-01
9
419 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
  • 3
  • 3
  • 2
9 Comments
 
LVL 25

Assisted Solution

by:devic
devic earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:pbrooks
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Accepted Solution

by:
NetGroove earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks C101.

Thank you pbrooks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now