Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY CYBERSECURITY DATA SCIENCE
     ❯   

HTML onunload Event Attribute

❮ HTML Event Attributes

Example

Execute a JavaScript when a user unloads the document:

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

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).


Browser Support

Event
onunload Yes Yes Yes Yes Yes

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


Syntax

<element onunload="script">

Attribute Values

Value Description
script The script to be run on onunload

❮ HTML Event Attributes