html tutorial - onbeforeunload Attribute in HTML - html5 - html code - html form




Onbeforeunload attribute in html

Learn html - html tutorial - Onbeforeunload attribute in html - html examples - html programs

  • The onbeforeunload attribute is used to specify when the document needs to be unloaded.
  • The onbeforeunload attribute allows the user to show a message in a confirmation dialog box to inform whether the user he/she needs to stay or leave the current page.
  • Hence the default message is appearing in the dialogue box. It is based on the different browsers.
  • We cannot remove the message but we can add a custom message in the dialogue box.

Syntax for onbeforeunload attribute in HTML:

<body onbeforeunload="script">

Applies To:

Element Attribute
<body> onbeforeunload

onbeforeunload Attribute Value:

Value Description
script The script can run on onbeforeunload.

Sample Coding for onbeforeunload Attribute in HTML:

Tryit<!DOCTYPE html>
<html>
    <head>
        <title>Wikitechy HTML onbeforeunload Attribute</title>
    </head>
    <body onbeforeunload="return onBeforeUnload()">
        <a href="http://www.wikitechy.com/">Click here to go to Wikitechy.com</a>
        <script>
         function onBeforeUnload () {
                return "Write something here...";
                }
        </script>
    </body>
</html>

Code Explanation for onbeforeunload Attribute in HTML:

onbeforeunload Attribute Code Explanation

  1. The onbeforeunload attribute fires when the document is about to be unloaded and the function onBeforeUnload() is called.
  2. href attribute defines the URL of the page which goes to that link ("http://www.wikitechy.com/")
  3. The function onBeforeUnload() is invoked when we press F5 or click on the link.

Output for onchange Attribute in HTML:

onbeforeunload Attribute Output

  1. The output shows the link to go to Wikitechy website.
  2. Once we click the link the dialogue box is opening with the message.
  3. If we click the Leave button, the next page is navigated to new page in same tab.
  4. If we click the stay button, the page is stay itself.
  5. onbeforeunload Attribute Output
  6. After clicking the leave button the page moves to wikitechy.com website.

Browser Support for onbeforeunload Attribute in HTML:

Yes Yes Yes Yes 15.0

Related Searches to onbeforeunload Attribute in html

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×