Before the current window is closed, check whether the current window is closed
<pre name="code"><pre name="code"><HTML><HEAD> <script Language="JavaScript"> window.onbeforeunload=function(event){ alert("222"); //IE9 will execute here, CHROME will not execute // if(event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){ // If it is refresh, it will not prompt RETURN "Confirm 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); //The window is not closed now, the result is false aa.close(); <span style="white-space:pre"> </span> //Calling the method of window closing console.log("not in wait and val is :"+aa.closed); //Aa.close is in the process of calling, and the result is false intervalVar = setInterval(showClose,100); }; //Use a loop to detect whether the subwindow is closed. In fact, it is OK to use setTimeout, but the value must be set larger function unloadform(){ alert("2!"); } </script> </HEAD><BODY OnLoad="loadform()" OnUnload="unloadform()"> </BODY></HTML>In ie9, if the document is refreshed, unloadform and onbeforeunload will be executed. If the page is closed, only onbeforeunload will be executed.
It is worth noting that onunload will not run when closing the page. It is estimated that this function is a built-in event in the browser and cannot be redefined.
In chrome, if the document is refreshed, both unloadform and onbeforeunload will be executed. If the page is closed, only onbeforeunload will be executed. It is worth noting that the commented line will not be executed.
If it is a child window opened by window.open, you can use the window.closed property to check whether it is closed.
<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); //The window is not closed now, the result is false aa.close(); //Calling the method of window closing console.log("not in wait and val is :"+aa.closed); //At this time aa.close is in the process of calling, the result is false intervalVar = setInterval(showClose,100); }; //Use loop to detect whether the child window is closed, in fact, it is OK to use setTimeout, but the value should be set larger function unloadform(){ alert("2!"); } </script> </HEAD><BODY OnLoad="loadform()" OnUnload="unloadform()"> <a href="test.htm">Calling</a> </BODY></HTML>Code that automatically closes window.open when the parent window is closed
<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>