Solved

how to check for the existence of a child window

Posted on 2003-11-25
7
763 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now