how to check for the existence of a child window

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
aiyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xabiCommented:
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
xabiCommented:
<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>
NetGrooveCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

James RodgersWeb Applications DeveloperCommented:
<script>

var myWin;

function checkWindow(){


}
James RodgersWeb Applications DeveloperCommented:
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">

VincentPugliaCommented:
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
aiyaAuthor Commented:
Thank u all for ya help!!! =)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.