Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML onunload Attribute


Definition and Usage

The onunload attribute fires once a page has unloaded (or the browser window has been closed).

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

Note: If you reload a page, you will also trigger the onunload event (and the onload event).


Applies to

The onunload attribute is part of the Event Attributes, and can be used on the following element:

Elements Event
<body> onunload

Example

Execute a JavaScript when a user unloads the document:

<body onunload="myFunction()">
Try it Yourself »

Browser Support

Event
onunload Yes Yes Yes Yes Yes

Note: Due to different browser settings, this event may not always work as expected.