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

archrajanCommented:
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
0
veedaAuthor Commented:

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



0
ZvonkoSystems architectCommented:
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.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

veedaAuthor Commented:


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>
0
ZvonkoSystems architectCommented:
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.

0
veedaAuthor Commented:


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
0
ZvonkoSystems architectCommented:
Lost is lost is lost.

After it is lost you have to check again.
Like this:

<HTML>


<HEAD>

<SCRIPT>

     function checkWindow(myName) {
          window[myName] = window.open("", myName, "top=9000,left=9000,height=100,width=100");
          if(window[myName].isOpen==true) return true;
          window[myName].close();
          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
               splitpolicy = window.open(theUrl, myName, property);
               splitpolicy.isOpen=true;
               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>

0

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
veedaAuthor Commented:
Wow, this works like a magic, excellent solution.
Thank you so much.......I really appreciate your help.

Thanks,
valmeida
0
ZvonkoSystems architectCommented:
:-)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.