JavaScript detects whether the window is off before the current window closes _javascript tips

Source: Internet
Author: User
Tags setinterval

Detects whether the current window is closed before the current window closes

<pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><HTML><HEAD> <script = "JavaScript" > Window.onbeforeunload=function (Event) {alert ("222");//Here IE9 will execute, Chrome will not execute//if (Event.clientx >document.body.clientwidth && event.clienty<0| | 
Event.altkey) {//If it is refreshed, do not prompt return "OK close window?"; 
} var AA; 
var Intervalvar; 
function Showclose () {Console.log ("Wait and Val:" +aa.closed); 
Clearinterval (Intervalvar); 
function Loadform () {aa=window.open (' foo.html ', ' windowname ', ' width=200,height=200,scrollbars=no '); Console.log ("Check close before op and val:" +aa.closed); The window is not closed now, and the result is false aa.close (); <span style= "White-space:pre" > </span>//Calling window Close method console.log And Val is: "+aa.closed); At this point aa.close is in the process of being invoked, the result is false Intervalvar = SetInterval (showclose,100); }; 
Use loop to detect whether the child window is closed, in fact, with settimeout is also possible, but the value to set a large number of function Unloadform () {alert ("2!");} </script> 
<HTML><HEAD> 
<script language= "JavaScript" > 
var aa; 
var Intervalvar; 
function Showclose () { 
console.log ("Wait and Val:" +aa.closed); 
Clearinterval (Intervalvar); 
} 
function Loadform () { 
aa=window.open (' foo.html ', ' windowname ', ' width=200,height=200,scrollbars=no '); 
Console.log ("Check close before op and val:" +aa.closed); The window is now not closed, and the result is False 
aa.close ();//The method to invoke the window to close 
console.log ("Not in" and "Val:" +aa.closed); At this point aa.close is in the process of being invoked, the result is false 
Intervalvar = SetInterval (showclose,100); Using loops to detect whether the child window is closed, in fact, settimeout is also possible, but the value to be set to a large number of 
function Unloadform () {alert ("2!");} 
</script> 
 
 

Code that automatically closes the window.open when the parent window closes

 <HTML><HEAD> <script language= "JavaScript" > var aa; 
var Intervalvar; 
Window.onbeforeunload=function (event) {aa.close (); 
return "Hello"; 
function Loadform () {aa=window.open (' test.htm ', ' windowname ', ' width=200,height=200,scrollbars=no '); 
}; 
function Unloadform () {alert (' 2! ');} </script>  
 
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.