Use javascript to always open the same subwindow and close the parent window while automatically closing all subwindows

Source: Internet
Author: User

I saw this article on the Internet today, but it is rarely used, but after all, some people still need this feature. Otherwise, this article will not be available. This article mainly solves the following problems:
Copy codeThe Code is as follows:
1. Click a link to open the new form. If the form has been opened, the focus is switched to the opened form. Otherwise, the new form is opened. Difficulty: How do I determine whether the form has been opened and the form will be Active?
2. How can I close all other open forms when a main form is closed?
Implementation points:
1. window. open will return the window object of the newly opened window.
2. Implement a simulated simple HashMap to store window objects of sub-Windows.
3. Search for the HashMap each time you open the HashMap to check whether the subwindow exists.
4. If yes, switch the focus (window. focus) directly ).
5. If it does not exist, open one.
6. For Step 4, it is possible that the subwindow has been closed. Therefore, we adopt some tips to call its focus (in fact, we can use any method) first. If an error occurs, we also open one.
7. When the parent is closed, traverse the HashMap and try to close all subwindows.
8. All operations are implemented in the parent window.
9. The entire implementation principle is actually very simple, as long as you need to be familiar with js and dhtml, and then pay attention to the detailed problem handling.

Currently, the IE 6 sp1 test is passed. FF is not suitable for use because it does not support window. focus.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <title> Parent window </ title> </pead> <body> Maintaining the Parent window and Multi Child windows as in WinForm funcions: <ol> <li> Open the same child window once. </li> <li> Close all the child windows when the parent window is closing. </li> </ol> Child window: <select id = "sltWins"> <option selected = "selected" value = "c1.htm"> #1 </option> <option value = "c2.htm"> #2 </option> </select> <input id = "btnOpen" type = "button" value = "Open the selected window" onclick = "openWin () "/> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Package File Download

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.