Requirement: The child window is opened by the Window.Open method, and the child window should also be closed when the main window is closed.
Implementation ideas:
1. Open the second parameter of the Subwindow function window.open (url,winname) Winname can uniquely identify the open window. So closing the Subwindow only requires using the Winname.close () function.
2. A page may have more than one child window. Therefore, an array is required to store all the child window objects. When off, iterate over the array.
3. The child window can also open the child window again. The infinite loop goes on. So it needs to be judged.
This requirement can be implemented in two ways.
Call the close function of the child window.
This method is easy to understand, but the actual implementation process finds that the browser's shutdown event is not. and need to be button click Close or shortcut key off, a little more trouble,
To close a child window recursively
This method is simple, the disadvantage is that all the window Storage sub-window object array needs the same name
The following is a child window method that uses recursion to close child windows and child windows
function Closesonwindow (Win) { for(VarIndex=0;Index<win.length;Index++){//If the window is closedif(win[Index].closed) {Continue; }//If the window does not have a child window that can be openedif(typeof (Win[Index].openedwindow) = ="undefined") {win[Index].Close();Continue; }if(win[Index].openedwindow.length==0) {win[Index].Close(); }Else{Closesonwindow (win[Index].openedwindow); win[Index].Close(); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
window.open Open child window, close all child windows