HTML | onbeforeunload Event Attribute
The onbeforeunload event run when the document is about to be unloaded. This event is used to allow to display a message in a dialog box to inform the user to he/she wants to stay or leave the current page.
Syntax:
<element onbeforeunload = "script">
Attribute Value: This attribute contains single value script and it runs when onbeforeunload event called. This event attribute is supported by <body> tag.
Example:
<!DOCTYPE html> <html> <head> <title>onbeforeunload attribute</title> <style> body { text-align:center; } h1 { color:green; } a { text-decoration:none; font-weight:bold; } </style> </head> <body onbeforeunload="return myFunction()"> <h1>GeeksforGeeks</h1> <h2>onbeforeunload attribute</h2> <script> function myFunction() { return "This document is ready to load"; } </script> </body> </html> |
Output:

Supported Browsers: The browser supported by onbeforeunload attribute are listed below:
- Chrome
- Internet Explorer
- Firefox
- Safari
- Opera 15.0 and above
Recommended Posts:
- HTML | DOM onbeforeunload Event
- HTML | onmouseout Event Attribute
- HTML | ononline Event Attribute
- HTML | oncontextmenu Event Attribute
- HTML | onclick Event Attribute
- HTML | ondblclick Event Attribute
- HTML | oninput Event Attribute
- HTML | onchange Event Attribute
- HTML | onload Event Attribute
- HTML | onwheel Event Attribute
- HTML | onmouseup Event Attribute
- HTML | onmousemove Event Attribute
- HTML | oninvalid Event Attribute
- HTML | ondragleave Event Attribute
- HTML | ontoggle Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



