HTML onunload event attribute

Example

Execute a JavaScript block when the user unloads the document:

<body onunload="goodbye()">

Try it yourself

Browser support

IE Firefox Chrome Safari Opera

All major browsers support the onunload attribute.

Definition and usage

The onunload attribute is triggered when the page is being downloaded (or the browser window has been closed).

onunload occurs when the user navigates away from the page (by clicking a link, submitting a form, or closing the browser window, etc.).

Note:If you reload the page, the unload event (and the onload event) will also be triggered.

Differences between HTML 4.01 and HTML5

None.

Syntax

<element onunload="script">

Attribute value

Value Description
script Script executed when onunload occurs.