Solved

how to check for the existence of a child window

Posted on 2003-11-25
7
765 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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