Javascript checks to see if the window is closed before the current window closes

  • 2020-03-30 04:03:04
  • OfStack

Before the current window closes, detect whether the current window is closed


<pre name="code" class="html"><pre name="code" class="html"><HTML><HEAD> 
<script Language="JavaScript"> 
window.onbeforeunload=function(event){ 
alert("222"); //Here IE9 will execute,CHROME won't
// if(event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){ // If it's refreshing , Don't tip  
RETURN " Make sure to close the window ?"; 
// } 
} 
var aa ; 
var intervalVar; 
function showClose(){ 
console.log(" wait and val is :"+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 is :"+aa.closed); //Now that the window is not closed, the result is false
aa.close();<span style="white-space:pre"> </span> //Invokes the window close method
console.log("not in wait and val is :"+aa.closed); //At this point aa.close is in the process of being called and the result is false
intervalVar = setInterval(showClose,100); }; //Check whether the child window is closed with a loop. SetTimeout is fine, but make the value larger
function unloadform(){ alert("2!"); } 
</script> 
</HEAD><BODY OnLoad="loadform()" OnUnload="unloadform()"> 
</BODY></HTML>

In ie9, both unloadform and onbeforeunload are executed if the document is refreshed, and only onbeforeunload events are executed if the page is closed

It is important to note that onunload does not run when pages are closed, and this function is estimated to be a browser built-in event and cannot be redefined

In chrome, both unloadform and onbeforeunload are executed if the document is refreshed, and only onbeforeunload is executed if the page is closed. It is worth noting that the commented line is never executed.

If it is a child window of window.open, you can detect whether it is closed through the window.closed property


<HTML><HEAD> 
<script Language="JavaScript"> 
var aa ; 
var intervalVar; 
function showClose(){ 
console.log(" wait and val is :"+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 is :"+aa.closed); //Now that the window is not closed, the result is false
aa.close(); //Invokes the window close method
console.log("not in wait and val is :"+aa.closed); //At this point aa.close is in the process of being called and the result is false
intervalVar = setInterval(showClose,100); }; //Check whether the child window is closed with a loop. SetTimeout is fine, but make the value larger
function unloadform(){ alert("2!"); } 
</script> 
</HEAD><BODY OnLoad="loadform()" OnUnload="unloadform()"> <a href="test.htm"> call </a> 
</BODY></HTML>

Code that automatically closes 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> 
</HEAD><BODY OnLoad="loadform()" OnUnload="unloadform()"> <a href="test.htm"> call </a> 
</BODY></HTML>

Related articles: