Window. open subwindow, close all subwindows, window. open window
Requirement: The subwindow is opened through the window. open method. When the main window is closed, the subwindow should also be closed.
Implementation ideas:
1. The second parameter winName of window. open (url, winName) in the subwindow function can uniquely identify the window to be opened. Therefore, you only need to use the winName. close () function to close the subwindow.
2. A page may have multiple subwindows. Therefore, an array is required to store all the Child Window objects. When the function is disabled, You can traverse the array.
3. You can open the subwindow again. Infinite Loop. Therefore, you need to determine.
This requirement can be implemented in two ways.
Call the close function of the subwindow.
This method is easy to understand, but the browser Close event is not found in the actual implementation process. You need to click the button to close or the shortcut key to close it, which is a little troublesome,
Recursively close subwindow
This method is easy to implement. The disadvantage is that the object arrays of all window storage subwindows must have the same names.
The following describes how to recursively close subwindows and subwindows.
Function closeSonWindow (win) {for (var index = 0; index <win. length; index ++) {// if the window is closed if (win [index]. closed) {continue;} // if the window does not have any subwindow that can be opened, if (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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.