X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

onunload Event

Event Object Reference Event Object

Example

Execute a JavaScript when a user unloads the document:

<body onunload="myFunction()">

Try it yourself »


Definition and Usage

The onunload event occurs 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: The onunload event is also triggered when a user reloads the page (and the onload event).


Browser Support

Event Attribute
onunload Yes Yes Yes Yes Yes

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


Syntax

In HTML:

<element onunload="myScript">

In JavaScript:

object.onunload=function(){myScript};

In JavaScript, using the addEventListener() method:

object.addEventListener("unload", myScript);

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.


Technical Details

Bubbles: No
Cancelable: No
Event type: UIEvent if generated from a user interface, Event otherwise.
Supported HTML tags: <body>
DOM Version: Level 2 Events


Event Object Reference Event Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook