Link to home
Start Free TrialLog in
Avatar of veeda
veeda

asked on

Count number of windows open from a parent window.



Hello,

 How do i count the number of windows (child) open from the parent window.

Thank you in advance.

Valmeida
Avatar of archrajan
archrajan

opener Property
When opening a window using window.open, use this property from the destination window to return details of the source window. This has many uses, for example, window.opener.close() will close the source window.

Syntax: window.opener
Avatar of veeda

ASKER


Sorry, I wasn't clear in my first email....
This is what I am trying to do :

- Open a child window from a parent window and allow the user to open only one child window at a time, display a message if trying to open more than one window.

This is coded and works fine, now the problem is when I open a child window and close the main/parent window leaving the child window open and launch the new parent window again, it does not detect the already open window(child) and it opens the window again.

So I thought may be I can find out how many windows opened by the parent and determine the closing of the main windows based on this.

Is there any other easier solution to this problem??

Thanks,
Veeda



Avatar of Zvonko
The solution for that problem is to use named windows.
When you open a window by window.open() then you can pass a second parameter for window name.
Now any next window.open() with the same window name will NOT open a new window: it will reuse the already opened window with the same window name.

Avatar of veeda

ASKER



Here is the code...........it works fine as long as I don't close the main/parent window.
If I close the parent window and leave the child window open and then open a new parent window, then split policy window exists alert returns false.......it does not recognize the already open child window from a previous parent  window.

Thx,
valmeida




<HTML>


<HEAD>

<SCRIPT>

      function checkWindow(myName) {

           try{if(eval("window."+myName+".document"))return true;}catch(e){}
           return false;
      }

      function popUp() {
            var theUrl   = "/onlinechange/onlinespchange.do?sp=yes";
            var myName   = "splitpolicy";
            var property = "width=800,height=800,status=no,resizable=yes,scrollbars=yes,toolbar=no,top=200,left=200";

            alert("Split Policy window exists : " + checkWindow("splitpolicy"));

            if(!checkWindow("splitpolicy")) {
                  // open a new window
                 eval(myName+" = window.open(theUrl, myName, property)");
                 splitpolicy.focus();
          } else { // if the window already open then display alert message
                      alert("There is already a split policy window open.");
                      splitpolicy.focus();
                }
      }

</SCRIPT>

</HEAD>

<BODY >

<input type="button" name="open" value="Open Windows" onclick="popUp();" />

Test page only.

</BODY></HTML>
Thats is all ok, but all I can bet with you is that you will never! have TWO windows opened with the name "splitpolicy"
That your check for the window name is failing because the reference is lost is normal, but the window.open('"", "splitpolicy") will not open two windows. IT will always open the last window which was opened with window.open('"", "splitpolicy")

All I want to say to you: you need NOT to check wether there was a previous window opened with window name"splitpolicy"; that check will be done by window.open() itself.

Avatar of veeda

ASKER



If I don't check for already opened "splitpolicy" window, it loads the application on existing window like you said, I don't want to do that, my application is a form and user will loose the enetered data if I don't check for already opened windows.

My problem is, when I close the parent window and keep the child window open, reference is lost and opening a new child window from a new parent window opens the application in an existing child window, i want to prevent this from happening as the user entered data is lost from the application. How can i get back the reference which is lost and check to see if there is a window open??

thanks,
valmeida
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of veeda

ASKER

Wow, this works like a magic, excellent solution.
Thank you so much.......I really appreciate your help.

Thanks,
valmeida
:-)