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>