function window.open Implementation closes all child windows _javascript tips

Source: Internet
Author: User

This article describes how to use the Window.Open method to open a child window, when we want to close the main window, this time all the child windows will also be closed. Below has my realization thought and the code, shares to everybody.

Realize the idea:
1. The second argument that opens the child window function window.open (url,winname) Winname can uniquely identify the open window. So closing a child window requires only using the Winname.close () function.
2. A page may have multiple child windows. Therefore, an array is required to store all child window objects. When closed, iterate through the array.
3. Child windows can also open child windows. Infinite loop down. So it needs to be judged.
This requirement can be implemented in two ways.

Calls the close function of a child window.
This method is easy to understand, but the actual implementation found that the browser shutdown event did not. and need to be button click off or shortcut key off, a little trouble, recursive close child windows
This method is simple to implement, with the disadvantage that all window storage child windows have the same name for the object array

The following is a child window method that uses recursion to close child and child windows

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 a child window 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 ();}}

The above is the whole description of this article, hope to help everyone.

Related Article

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.