html tutorial - onunload Attribute in HTML - html5 - html code - html form
- The onunload attribute is used to execute a script when a page has unloaded.
- The onunload happens when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.)
- The onunload attribute is a section of the Event Attributes.
Syntax for onunload attribute in HTML:
<body onunload="script">
Applies to:
Elements | Attribute |
---|---|
<body> | onunload |
Attribute values:
Value | Description |
---|---|
Script | The script can run on onunload. |
Sample coding for onunload attribute in HTML:
<!DOCTYPE html>
<html>
<body onload="Wikitechy()">
<h1><Wikitechy!></h1>
<script>
function Wikitechy() {
alert("Wikitechy Page is loaded");
}
</script>
</body>
</html>
Code Explanation for onunload attribute:

- <body> tag encloses all the contents of an HTML document.
- The onunload attribute executes when an associated element has unloaded.
Browser Support for onunload attribute in HTML:
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes |
Tips and Notes:
- If we reload a page, we will also trigger the onunload event and the onload event.