There are many browser window close events. Here we will introduce several common browser window close events. For example, if you close a window, a prompt box is displayed, or the window. onbeforeunload and window. onunload events are displayed before closing the window.
Browser Close event
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript">
Window. onbeforeunload = onbeforeunload_handler;
Window. onunload = onunload_handler;
Function onbeforeunload_handler (){
Var warning = "are you sure you want to exit? ";
Return warning; // you can change it to alert or another specific operation.
}
Function onunload_handler (){
Var warning = "thank you for coming ";
Alert (warning); // close and run
}
</Script> |
Close the browser (no prompt box is displayed
| The Code is as follows: |
Copy code |
|
<Script language = "javaScript">
Function closeWindow ()
{
Window. opener = null;
Window. open ('', '_ self ','');
Window. close ();
}
</Script>
<Input type = 'button 'value = 'close window' onClick = "closeWindow ()"> |
Iframe Close Window
| The Code is as follows: |
Copy code |
|
<Script language = "javaScript">
Function closeWindow ()
{
Window. opener = null;
Window. open ('', '_ top ','');
Window. parent. close ();
}
</Script>
|