Open In App

HTML onunload Event Attribute

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:

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




<!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 : 

Article Tags :