Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Scripting > Javascript   »   how to close all the childwindows(popup window) when closing the parent window

 
Thread Tools Display Modes
Question
 
 
#1  
Old Jul 9, 2007, 05:54 AM
krisamigo
New Member
krisamigo is offline
 
Join Date: Jul 2007
Posts: 1
krisamigo See this member's comment history on his/her Profile page.
how to close all the childwindows(popup window) when closing the parent window

hi, all

in javascript
i need to close all the child windows[popup windows] that i have opened through a parent window

scenario:
here i will be having as many as child window opened and name given to the child window can not be tracked because each will be having a different name, and

i am using 70 to 80 popup windows [doing seperate operations] through out my page [parent window]

there can be any number of child windows opened , at the time of user triGgers the event from the parent window to close all the child window that i have opened so far [currently opened child windows]

Reply With Quote
 
     

Answers
 
 
Old Jul 16, 2007, 05:37 AM   #2  
Full Member
jstrike is offline
 
Join Date: May 2007
Location: Wisconsin - Go Packers!
Posts: 391
jstrike See this member's comment history on his/her Profile page.
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.

Code:
<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);
            }
        
        </script>

        <input type="button" value="Open 1" onclick="open1()"><br>
        <input type="button" value="Open 2" onclick="open2()"><br>
        URL: <input type="text" id="url">&nbsp;<input type="button" value="Open URL" onclick="open3()"><br>
        <input type="button" value="Close All" onclick="closeWindows()">

    </body>
</html>
Hope this helps.
  Reply With Quote
 
     
 
 
Old Aug 15, 2008, 07:25 AM   #3  
New Member
rayabharapusuma is offline
 
Join Date: Aug 2008
Posts: 1
rayabharapusuma See this member's comment history on his/her Profile page.
Quote:
Originally Posted by jstrike
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.

Code:
<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);
            }
        
        </script>

        <input type="button" value="Open 1" onclick="open1()"><br>
        <input type="button" value="Open 2" onclick="open2()"><br>
        URL: <input type="text" id="url">&nbsp;<input type="button" value="Open URL" onclick="open3()"><br>
        <input type="button" value="Close All" onclick="closeWindows()">

    </body>
</html>
Hope this helps.
I will open a report from a button click event and i want to close the same window when i user clicks to generate the report again.

One Button ----> click -----> report generation(in popped up page)
for pop up page i use clientsidescript.registerstartupscript in c#.net
now he goes to parent page and selects other report then i have the already popped up windows creating problem sometimes. Please let me know to use the same to open for the first time.

close it and open the same for other or same report.

Thanks,
Suman
  Reply With Quote
 
     
 
 
Old Aug 15, 2008, 11:36 AM   #4  
Full Member
jstrike is offline
 
Join Date: May 2007
Location: Wisconsin - Go Packers!
Posts: 391
jstrike See this member's comment history on his/her Profile page.
Give the window a name and use the same name every time you opent he report. It will automatically overwrite the old window.

open(url,"report");
  Reply With Quote
 
     


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
close all child windows thru parent window srinivask12 Javascript 1 Jul 16, 2007 05:38 AM
close all child windows thru parent window zybernau Other Web Development 0 Mar 22, 2007 09:46 AM
How to close a power window in open position? TR2007 Cars & Trucks 3 Mar 2, 2007 05:01 PM
PopUp Window on Task Bar, ONLY kflions99 Spyware, Viruses, etc. 2 May 12, 2006 11:26 AM
Old wooden window won't close mikestorm Exterior Home Improvement 8 Dec 16, 2005 02:01 PM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 05:17 PM.