Last Updated : 11 Jul, 2025
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:
<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 BrowsersThe browser supported by onunload event attribute are listed below :
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4