HTML onunloadEvent Attributes
Examples
Execute JavaScript when the user closes a page:
<body onunload="goodbye()">
Try it Yourself »
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the onunload event attribute.
Definition and Usage
The onunload attribute fires when the user closes a page.
onunload fires when the user leaves the page (by clicking a link, submitting a form, closing the browser window, etc.).
Note:If you reload the page, the unload event will be triggered (onloadevent).
Differences Between HTML 4.01 and HTML5
None.
Syntax
<element onunload="script">
Attribute Values
| Value | Description |
|---|---|
| script | Specifies the script to be executed when the onunload event is triggered. |
HTML Event Attributes Other Extensions