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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>   
        <a href="https://www.geeksforgeeks.org">Go to GeeksforGeeks</a>
        <script>
        function myFunction() {
            return "This document is ready to load";
        }
        </script>
    </body>
</html>

chevron_right


Output:

Supported Browsers: The browser supported by onbeforeunload attribute are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera 15.0 and above


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.