onunload event

Definition and usage

The onunload event occurs once the page has been unloaded (or the browser window has been closed).

The onunload event occurs when the user leaves the page (by clicking a link, submitting a form, closing the browser window, etc.).

Bemærk:Also triggers the onunload event when the user reloads the page (and onload begivenhedAlso triggers the onunload event when ) is closed.

Example

Execute JavaScript when the user unloads the document:

<body onunload="myFunction()">

Try it yourself

Syntax

In HTML:

<element onunload="myScript">

Try it yourself

In JavaScript:

object.onunload = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("unload", myScript);

Try it yourself

Bemærk:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Not supported
Cancelable: Not supported
Event type: If generated from the user interfaceUiEventOtherwise Event.
Supported HTML tags: <body>
DOM version: Level 2 Begivenheder

Browserstøtte

Begivenheder Chrome IE Firefox Safari Opera
onunload Support Support Support Support Support

Bemærk:På grund af forskellige browserindstillinger kan denne begivenhed ikke altid fungere som forventet.