The child window closes when the JS parent window closes the perfect solution _javascript tips

Source: Internet
Author: User
Recently, a privilege management system was encountered. Because the system of rights management and the original system style is inconsistent, all new open a window. The problem is, admin logout, the Rights Management window is not closed. After other normal users log on, they can still manipulate the Rights Management window.

Problem simplification: When admin logs off, or when main.html closes, all new windows that are open are closed together. The problem is solved.

Just look at the code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body onunload= "closenews ();" >

<a href= "#" onclick= "opennew (' http://www.baidu.com ', ' Nihao ')" > Open new Window </a>
<a href= "#" onclick= "LogOut ();" > Exit Login </a>
</body>

<script>
The user records all open child windows
var win_array = new Array ();
var win_num =-1;

Every time you open a new window, it's recorded.
function Opennew (uri,param) {
Win_num = win_num+1;
Win_array[win_num] = window.open (URI);
}

When the user logs off, the Shutdown method is performed. Body Add onunload = "closenews ();" When main window closes, all child windows are closed
function Closenews () {
Turn off Rights Management
if (Win_array.length > 0) {
for (var i = 0;i <= win_array.length;i++) {
var win_one = win_array[i];
if (win_one!= undefined) {
Win_one.close ();
}
}
}
}

function LogOut () {
Close child windows
Closenews ();

Exit Login
}

</script>

Explain:

<body onunload= "closenews ();" > Adding onunload events to the body is important. That is, when the window closes, execute the method of closing all child windows.

Test: In the Web project, Ie8,ie10, Firefox, Chrome,opera are all fine.

Once the Wen container is not put down, just static main.html page, IE has a problem, because the static, ie window.open () is actually open a new page, not open a new label. All methods that close the window are no longer valid.

Time is limited, has not spent energy to examine, but also hope to know why the JS technology can guide the big God. Thanks again.

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.