onunloadEvent

事件对象参考手册Event Object

Examples

JavaScript code executed when the user unloads the document:

<body onunload="myFunction()">

Try it Yourself »

Definition and Usage

The onunload event occurs when the user exits the page.

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

Note:The onunload event also triggers the page load event (onloadonloadevent).

Syntax

In HTML:

<body onunload="SomeJavaScriptCode">

In JavaScript:

window.onunload=function(){SomeJavaScriptCode};

Parameters Description
SomeJavaScriptCode Required. Specifies the JavaScript to execute when the event occurs.


Browser Support

Event
onunload Yes Yes Yes Yes Yes

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


The following HTML tags support onunload:

<body>, <frameset>


事件对象参考手册Event Object Other Extensions