[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

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
0
veeda
Asked:
veeda
  • 4
  • 4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now