Open In App
Related Articles

HTML onunload Event Attribute

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The onunload event attribute works when the document is being unloaded i.e. it occurs when the browser has been closed. It is mostly used when the user opens a link and submits the form and closes the browser window. Basically, it is:

  • Fired when a user navigates away from a page or closes the browser.
  • Useful for executing cleanup actions or displaying confirmation prompts.
  • Allows to handle scenarios before the page unloads.

Supported Tags

Syntax

<element onunload = "script">

Attribute Value

This attribute contains a single value script that works when onunload event is triggered.

Example: In this example, we will see the use of onunload event attribute

HTML

<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML onunload Event Attribute
    </title>
    <script>
        function Geeks() {
            alert("onunload event attribute called");
        }
    </script>
</head>
 
<body onunload="Geeks()">
    <h1>
        GeeksforGeeks
    </h1>
    <h2>
        onunload Event Attribute
    </h2>
</body>
 
</html>

                    

Output: 

Note: This event may not work always as expected.

Supported Browsers

The browser supported by onunload event attribute are listed below : 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Opera 4 and above
  • Safari 3 and above

Last Updated : 15 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads