Page view counter

Closing the Child window on closing the Parent

Last post 10-03-2008 7:36 AM by NC01. 10 replies.

Sort Posts:

  • Closing the Child window on closing the Parent

    07-11-2008, 1:04 AM
    • Loading...
    • jayachitra
    • Joined on 01-22-2008, 4:10 AM
    • India
    • Posts 30
    • Points 12
     Dear All,

    In my web applicaiton, on clicking a menu, I am displaying the relevant information by opening the child window using java script.

    If the parent window is closed / the user logged out, then the child window also has to be closed.

    Can any one tell me a way to perform this?

    Thanks a lot in advance. Smile

  • Re: Closing the Child window on closing the Parent

    07-11-2008, 2:07 AM
    Answer
    • Loading...
    • Maulik Patel
    • Joined on 12-21-2007, 6:48 AM
    • Ahmedabad
    • Posts 324
    • Points 2,294

    Create an array to track the open windows.
    Whenever you open a window, pass the handle to that window to a function that will add it to the array. When you want to close all the windows, loop through the array and close them. You don't need to worry if a child window is closed or not. The following code was quickly tested on IE 6 and FF 2. The closeWindows function would need to be called in the onunload event.

     
    <html>
        <head></head>
        <body>
            <script>
                //The openWindow array will hold the handles of all open child windows
                var openWindow = new Array();
                
                //Track open adds the new child window handle to the array.
                function trackOpen(winName) {
                    openWindow[openWindow.length]=winName;
                }
                
                //loop over all known child windows and try to close them.  No error is
                //thrown if a child window(s) was already closed.
                function closeWindows() {
                    var openCount = openWindow.length;
                    for(r=0;r<openCount;r++) {
                        openWindow[r].close();
                    }
                }
                
                //Open a new child window and add it to the tracker.
                function open1() {
                    var win1 = open("http://www.yahoo.com");
                    trackOpen(win1);
                }
            
                //Open a different child window and add it to the tracker.
                function open2() {
                    var win2 = open("http://www.google.com");
                    trackOpen(win2);
                }
                //Open whatever the user enters and add it to the tracker
                function open3() {
                    var newURL = document.getElementById("url").value;
                    var win3=open(newURL);
                    trackOpen(win3);
                }
                
            window.onunload = function closeALLWindows()
                                {
                                    closeWindows();
                                }
            input type="button" value="Open 1" onclick="open1()"><br>
            <input type="button" value="Open 2" onclick="open2()"><br>
            URL: <input type="text" id="url"> <input type="button" value="Open URL" onclick="open3()"><br>
            <input type="button" value="Close All" onclick="closeWindows()">
    
        </body>
    </html> 
     
    Maulik Patel
    MCTS, Software Engineer

    Don't forget to click "Mark as Answer" on the post that helped you. This will give you point and help readers to know which post solved your issue and make their search easy.
  • Re: Closing the Child window on closing the Parent

    07-11-2008, 2:08 AM
    Answer

    <body onunload="closeWins();">

    var popSuite; 

     function closeWins()
     {
         if (popSuite!=null)
         {
            if (!popSuite.closed)
              popSuite.close();
         }
    }

    popSuite is nothing but u r using that child Window Index Name that is PopSuite.. Ex:

    popSuite = window.open('AddBanner.aspx'......);

    Hope this helps...

    IF THIS POST HELPS YOU, CLICK MARK AS ANSWER

  • Re: Closing the Child window on closing the Parent

    07-21-2008, 6:33 AM
    • Loading...
    • roopkt
    • Joined on 06-21-2006, 6:18 AM
    • Mumbai
    • Posts 82
    • Points 89

    at this code onunload="closeWins();"

    i am gettingone error "object doesnot support this action"

    and also child page is opening multiple times.

    please suggest me where i am wrong?

    Rupesh kumar Tiwari
    India
  • Re: Closing the Child window on closing the Parent

    07-21-2008, 6:55 AM
    • Loading...
    • roopkt
    • Joined on 06-21-2006, 6:18 AM
    • Mumbai
    • Posts 82
    • Points 89

    I found that 2 times onuload event of parent page is firing.

    1st when the child page is getting open.

    2nd when the parent page is being closed.

    And I am getting error on 1st time onuload event that "object does not support this action"

    my Code is :

    Parent.aspx

    <script type="text/javascript">

    var child 

    function openChild()

    {

    child = window.open("child.aspx"); 

    fuction closeChild()

    {

    if(child &&  !child.closed())

    {

    child.close();

    }

    }

    </script>

    <body onuload="closeChild();">

    <asp:Button onclientClick="javascript:openChild();" runat="server" id="btn" text="open Child"/>

    </body>

    Rupesh kumar Tiwari
    India
  • Re: Closing the Child window on closing the Parent

    07-21-2008, 7:32 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391

    Here you are. Just remember that if the parent window does a PostBack, the reference to the child window will be lost and this will no longer work.

    Parent.aspx

    <form id="Form1" method="post" runat="server">
     <asp:Button onclientClick="javascript:openChild();" runat="server" id="btn" text="Open"/>
    </form>

    <script type="text/javascript">
    <!--
    var childWindowRef = null;

    function openChild()
    {
     if ( (childWindowRef == null) || (childWindowRef.closed == true) )
     {
      childWindowRef = window.open("Child.aspx");
      childWindowRef.focus();
     }
    }
    function closeChild()
    {
     if ( (childWindowRef) && (childWindowRef.closed == false) )
     {
      childWindowRef.close();
     }

     childWindowRef = null;
    }
    window.onunload = closeChild;
    // -->
    </script>

    Child.aspx

    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="window.close();" value="Close">
    </form>

    <script type="text/javascript">
    <!--
    window.onunload = function ()
    {
     window.opener.closeChild();
    }
    // -->
    </script>

    NC...

     

  • Re: Closing the Child window on closing the Parent

    07-29-2008, 1:57 AM
    • Loading...
    • roopkt
    • Joined on 06-21-2006, 6:18 AM
    • Mumbai
    • Posts 82
    • Points 89

    hey,

    I was doing one mistake i got the same it is :

    if(child &&  !child.closed())

    I was using above code instead when i used if(child &&  !child.closed) then it worked fine!!!

    Thanks all of you for your help...

     

    Rupesh kumar Tiwari
    India
  • Re: Closing the Child window on closing the Parent

    10-03-2008, 5:14 AM
    • Loading...
    • nevincm
    • Joined on 07-10-2007, 11:05 AM
    • Posts 127
    • Points 342

     hi ,

    i appreciate your logic  .

    I tried this code for my application .It worked in almost all situations . But in one or two case's it's not working properly.

    In my application a main page is there .It contains 8 frames and almost 6 hyperlinks

     

    When i open popup from  hyperlink and i click logout it's not closing the popup opened from the hyperlink .

    when i open a popup from hyperlink it is added in the array. But it's not getting closed 

    If i opens a popup  from pages in frame . i can close it using Array's

    If you know what is the problem ,please help me

     

    thanks & regards

    nevviin

     

     

    Thanks & Regards
  • Re: Closing the Child window on closing the Parent

    10-03-2008, 7:16 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391

    Frames are like separate windows, and I doubt if you can make the code that I posted work because wouldn't window.opener point to the frame and not the window hosting the frame? You should only use frames when you absolutely have to -- Hosting a page that belongs to someone else or some such requirement. You definitely should not be opening child windows from them if you need to control the child window from the parent.

    NC...

  • Re: Closing the Child window on closing the Parent

    10-03-2008, 7:30 AM
    • Loading...
    • nevincm
    • Joined on 07-10-2007, 11:05 AM
    • Posts 127
    • Points 342

     hi

     

    thanks for reading my post .

    But actulay the problem is not from the  frames . 

    There are six hyperlinks on th the top of the page. That is not inside the frames . they are placed in tables  just like this 

    <td><a href="javascript:openHlink('Accounts','InOrganic')" style="color:#FFFFFF;"><strong> New (InOrganic)</strong></a></td>

     

     when i click the hyperlink it calls the openHlink()

    function openHlink(tab,sel,colval)
        {
            var imgbtPos; 
        try
            {
            //var span = document.getElementById("Chart1");
              
                //alert('openHlink')
                myleft=(screen.width)?(screen.width-600)/2:100;mytop=(screen.height)?(screen.height-500)/2:100;
                settings="width=" + 600 + ",height=" + 500 + ",top=" + mytop + ",left=" + myleft + ",scrollbars=auto,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
                win2=window.open("Hlink.aspx?TAB="+tab+"&SEL="+sel+"&COL="+colval,"Hlink",settings);
                trackOpen(win2);

             
               
               
            }
        catch(err)
            {
                alert(err);
                return false;
            }
       
        }   
        

     

     

    function trackOpen(winName) {
                 alert('inside trackOpen');
                alert('len'+openWindow.length);
                try{
               
                        openWindow[openWindow.length]=winName;
                        var openCount = openWindow.length;
                          alert('rc= '+ openCount);
                    }
                catch(err)
                  {
                 alert('trackopenerr');
                  }
               
               
             }

     

     

    In the  trackOpen fun i placed alert to know the  array length .But the rowcount still same that means the 'len' and 'rc' are same

    I dont know when the mainpage load itself the arraylength is 3 . I am not calling the trackopen  anywhere .

    Somehow it's length bcomes 3 and 

    But if calls from other frames the length is 0 before entering the trackopen and once get inside it will increase 

     

    i couldn't find any error .Only for this hyperlink it's not inserting the window object into array

    i am stuck with this problem, if anybody can point out the error please...

     

    thanks & regards

     

    nevviin

     

     

     

    Thanks & Regards
  • Re: Closing the Child window on closing the Parent

    10-03-2008, 7:36 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391

    nevincm,

    Please start a new thread with exactly what your requirements are and I will try and help you. It is not good practice to hijack someone else's thread, especially when that thread has been closed out (see the check mark in the thread listing?).

    NC...

     

Page 1 of 1 (11 items)