window.open Open child window, close all child windows

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.