HTML | DOM onbeforeunload Event

The onbeforeunload event occurs when you click a text link or picture link or any kind of link which will bring you to a new page. This event will display a confirmation dialog box to inform the user whether the user wants to stay on the page or leave the current page move on to the next linked page. The message in dialog box cannnot be removed.

Syntax:

  • In HTML:
    <element onbeforeunload="myScript">
  • In JavaScript:
    object.onbeforeunload = function(){myScript};
  • In JavaScript, using the addEventListener() method:
    object.addEventListener("beforeunload", myScript);

Below examples illustrate the onbeforeunload Event in HTML DOM:

  • Example: Using HTML
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>HTML | DOM onbeforeunload attribute</title>
        <style>
            body {
                text-align: center;
            }
              
            h1 {
                color: green;
            }
              
            a {
                text-decoration: none;
                  
            }
        </style>
    </head>
      
    <body onbeforeunload="return myFunction()">
        <h1>GeeksforGeeks</h1>
        <h3>HTML | DOM onbeforeunload attribute</h3>
        <p>Go to
            <a href="https://ide.geeksforgeeks.org/">GeeksgorGeeks </a>
        ide</p>
        <script>
            function myFunction() {
                return "This document is ready to load";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Ouput:
    After loading the code:

    After clicking the text link:

  • Example: Using Javascript
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>HTML | DOM onbeforeunload attribute</title>
        <style>
            body {
                text-align: center;
            }
              
            h1 {
                color: green;
            }
              
            a {
                text-decoration: none;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
        <h3>HTML | DOM onbeforeunload attribute</h3>
        <p>Go to
            <a href="https://ide.geeksforgeeks.org/">GeeksgorGeeks </a
        ide</p>
        <script>
            window.onbeforeunload = function(event) {
                event.returnValue = "This document is ready to load";
            };
        </script>
    </body>
      
    </html>

    chevron_right

    
    

    Ouput:
    After loading the code:

    After clicking the text link:

  • Example:In JavaScript, using the addEventListener() method:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>HTML | DOM onbeforeunload attribute</title>
        <style>
            body {
                text-align: center;
            }
              
            h1 {
                color: green;
            }
              
            a {
                text-decoration: none;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
        <h3>HTML | DOM onbeforeunload attribute</h3>
        <p>Go to
            <a href="https://ide.geeksforgeeks.org/">GeeksgorGeeks </a
        ide</p>
        <script>
            window.addEventListener("beforeunload", function(event) {
                event.returnValue = "This document is ready to load";
            });
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Ouput:
    After loading the code:

    After clicking the text link:

Supported Browsers:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




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.