Ask Experts Questions for FREE Help !
Ask

How to close all the childwindows(popup window) when closing the parent window

Asked Jul 9, 2007, 06:54 AM — 3 Answers
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 separate 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]

3 Answers
jstrike's Avatar
jstrike Posts: 418, Reputation: 223
Full Member
 
#2

Jul 16, 2007, 06:37 AM
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.
Helpful
rayabharapusuma's Avatar
rayabharapusuma Posts: 1, Reputation: 1
New Member
 
#3

Aug 15, 2008, 08:25 AM
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
Helpful
jstrike's Avatar
jstrike Posts: 418, Reputation: 223
Full Member
 
#4

Aug 15, 2008, 12:36 PM
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");
Helpful

Not your question? Ask your question View similar questions

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Add your answer here.

Remove Text Formatting

Undo
Redo
 
Decrease Size
Increase Size
Bold
Italic
Underline
Align Left
Align Center
Align Right
Ordered List
Unordered List
Decrease Indent
Increase Indent
Insert Email Link
Wrap [QUOTE] tags around selected text
Wrap [CODE] tags around selected text
Wrap [HTML] tags around selected text
Wrap [PHP] tags around selected text
Wrap [YOUTUBE] tags around selected text
Notification Type:



Check out some similar questions!

Close all child windows through parent window [ 1 Answers ]

Hi, all In javascript I need to close all the child 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...

Close all child windows through parent window [ 1 Answers ]

I want answer for the above question asapp

How to close a power window in open position? [ 3 Answers ]

'93 Integra - p/s window is in open position. I am waiting for a new Master Switch for the driver's side because I read that this was common fault w/problems. If this doesn't work, anyone help w/how to jump the motor to close the window? Also, how can I check if motor has gone south on me w/o...

PopUp Window on Task Bar, ONLY [ 2 Answers ]

Every so often I get this blue bordered window on my task bar that is white or blank inside, if you will, with no description. It loads quickly than goes. Now I do NOT get any corresponding window on my desk top just on the task bar and my computer stops while this thing loads for the two seconds...

Old wooden window won't close [ 8 Answers ]

I have a wooden window that jams about 6" from the bottom. It seems to jam on the left side and there's a small pile of sawdust in that corner. It doesn't look like the individual sliding parts of the window can be removed. Any advice?


View more Javascript questions Search