Skip to content
Related Articles

Related Articles

Improve Article

HTML | onbeforeunload Event Attribute

  • Difficulty Level : Basic
  • Last Updated : 30 Jul, 2021

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.
Supported Tags 

  • <body>


<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.


<!DOCTYPE html>
        <title>onbeforeunload attribute</title>
            body {
            h1 {
            a {
    <body onbeforeunload="return myFunction()">
        <h2>onbeforeunload attribute</h2>  
        <a href="">Go to GeeksforGeeks</a>
        function myFunction() {
            return "This document is ready to load";


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

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


Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :