Execute a JavaScript when the page is about to be unloaded:
Reload this page, or click on the link below to invoke the onbeforeunload event.
<!DOCTYPE html> <html> <body onbeforeunload="return myFunction()"> <a href="https://www.java2s.com">Click here to go to java2s.com</a> <script> function myFunction() {/*ww w .j a v a 2 s .co m*/ return "Write something clever here..."; } </script> </body> </html>
The onbeforeunload event occurs when the document is about to be unloaded.
Bubbles: | No |
---|---|
Cancelable: | Yes |
Event type: | UiEvent if generated from a user interface, Event otherwise |
Supported HTML tags: | <body> |