HTML onunload Event Attribute

Example

Execute a JavaScript 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 downloaded (or the browser window is 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 runs when onunload occurs.