Solved

how to check for the existence of a child window

Posted on 2003-11-25
7
767 Views
Last Modified: 2012-05-04
Hi,
  I am debugging a script below:
  function msgopen()
{
            var i=0;
            var j=0;
            var a_position =0;
            var final_index ="";
            var final_index_2="";
            var win_open=0; //indicate window opened or not
            var ref_array;
            var win_display;
            var isIE_browser = (navigator.userAgent.indexOf("IE") !=-1); //identify the browser

is IE or not
            var actual_text ="";
            
            if (win_open==0) //if display window does not exist, then create one
            {
                  if (isIE_browser) //for IE
                  {
                        //open a window to display results
                        win_display = open('', 'Timetable',

'scrollbars=yes,menubar=yes,resizable=yes,height=470,width=900');
                        win_open=1; //opened
                  }
                  else //for netscape
                  {
                        //open a window to display results
                        win_display = open('', 'Timetable',

'scrollbars=yes,menubar=yes,resizable=yes,height=470');
                        win_open=1; //opened
                  }
      
            }
            win_display.document.close();
              win_display.document.open();
              win_display.focus();
...
What I wish to do is to test for the presence of a child window, but seems like whenever the function is called win_open will always be initialised to 0, and it will recreate my table in the new window. What i want is that when a new window is created with a table in it, it will not reconstruct again and again when function is called. I wish to open the new window only once. How can i resolve this? Btw, how is it that we can pass parameters from one window to another? Pls help...
Million thanks,
aiya
0
Comment
Question by:aiya
7 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 9823043
Hi aiya,

1st of all this line:

var win_open=0; //indicate window opened or not

must be out of the function, cause if you call the funcion again you will reset the value of the variable.

I'm writing you the whole code.

xabi
0
 
LVL 8

Expert Comment

by:xabi
ID: 9823081
<html>
<head>
<script>
var oWindow = null;

function msgopen() {
  var i=0;
  var j=0;
  var a_position =0;
  var final_index ="";
  var final_index_2="";
  var ref_array;
  var win_display;
  var isIE_browser = (navigator.userAgent.indexOf("IE") !=-1); //identify the browser is IE or not
  var actual_text ="";
 
  if (!oWindow || oWindow.closed) {
    if (isIE_browser) {
      oWindow = window.open('', 'Timetable', 'scrollbars=yes,menubar=yes,resizable=yes,height=470,width=900');
    } else {
      oWindow = window.open('', 'Timetable', 'scrollbars=yes,menubar=yes,resizable=yes,height=470');
    }
  }
  oWindow.document.close();
  oWindow.document.open();
  oWindow.document.write("hello world");
  oWindow.focus();
}

function check() {
   alert(oWindow.closed ? "closed" : "opened");
}
</script>
</head>
<body>
<a href="javascript:msgopen()">Open the window</a><br>
<a href="javascript:check()">Check if it is opened.</a>
</body>
</html>
0
 
LVL 10

Accepted Solution

by:
NetGroove earned 250 total points
ID: 9823407
And here is my way of doing that:

<html>
<head>
<script>
function msgopen(){
         var i=0;
         var j=0;
         var a_position =0;
         var final_index ="";
         var final_index_2="";
         var win_open=0; //indicate window opened or not
         var ref_array;
         var win_display;
         var isIE_browser = (navigator.userAgent.indexOf("IE") !=-1); //identify the browser is IE or not
         var actual_text ="";
         var winParams = 'scrollbars=yes,menubar=yes,resizable=yes,height=470';
         if (isIE_browser) {
           winParams += ',width=900';
         }
         win_display = open('', 'Timetable', winParams);
         win_open = win_display.win_open;
         if (win_open==0) //if display window does not exist, then create one
         {
           // do here your init busines, like writing to win_display or setting gloval vars in win_display
           // or setting win_display.location to some page
           win_display.win_open=1; //opened
         }
         win_display.document.close();
         win_display.document.open();
         win_display.focus();
}
</script>
</head>
<body>
<form>
<input type=button value="Pop" onClick="msgopen()">
</form>
</body>
</html>



Defining the oWindow outside of the function, or even omitting the var clause in front of oWindow assignment does make the oWindow variable global.
But my philosophy is that the best place to store the window state is directly in the opened window.
That does say, you can always call window.open('', 'Timetable') to regain the access to the 'Timetable' window. It does not cost extra if it is already open and it release you of loosing the window reference for example after that window did some submits to the server.
Same as storing the global state variable directly to the 'Timetable' window, you can store other variables and parameters in the 'Timetable' window and even call functions in that window to process the stored parameters.

Good luck,
NetGroove


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:James Rodgers
ID: 9824402
<script>

var myWin;

function checkWindow(){


}
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9824425
sorry hit tab-enter

<script>

var myWin;

function checkWindow(){
   if(!myWin){
    alert("window was not opened");
  }else if(!myWin.closed){
       alert("window is open");
  }else{
    alert("window was open but is now closed");
  }
 
}
myWin=window.open("url","","");
</script>

<input type="button name="butCheck" onClick="checkWindow()" value="Check window">
<br>
<input type="button" onClick="if(myWin && ! myWin.closed) myWin.close();" value="close window">

0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9825800
Hi,

  Insofar as opening/closing the window: if you only want 1 popup, Jester's solution is cleaner and traditional -- keeping track with a var (win_open = 0) would be valid if you wanted more than one window.

>> Btw, how is it that we can pass parameters from one window to another?

as long as you have a window handle -- var newWin = window.open(...) -- you have a connection between the windows.  You can then access variables in either window without having to pass them in the location string (window.open('somepage.html?var1=3'....) as in the following:

in the opener window:
var myVar = someFunction();
var newWin = window.open(.....)
newWin.document.forms[0].elements[0].value = document.forms[0].elements[5].value
newWin.document.forms[0].elements[1].value = myVar;

in the child window (newWin):
document.forms[0].elements[0].value = opener.document.forms[0].elements[5].value
document.forms[0].elements[1].value = opener.window[myVar];


Vinny
0
 

Author Comment

by:aiya
ID: 9835202
Thank u all for ya help!!! =)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

809 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