onunload event


onunload Event

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function myFunction(){
	alert("谢谢访问W3C网站!");
}
</script>
</head>

<body onunload="myFunction()">
<h1>欢迎来到我的主页</h1>
<p>关闭窗口或者按F5刷新页面</p>

</body>
</html>

Run Instance»

Click "Run instance" button to view the online instance


Definition and usage

The onunload event occurs when the user exits the page.

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

Note: onunload event The page loading event (+ onload event) is also triggered.

Syntax

HTML:

<body onunload="SomeJavaScriptCode">

JavaScript in:

window.onunload=function(){SomeJavaScriptCode};

##ParameterDescriptionRequired. Specifies the JavaScript to be executed when this event occurs.
SomeJavaScriptCode


Browser support

QQ截图20161108165429.png##IE, Firefox, and Safari support the onunload event, but This event is not supported by Chrome or Opera.

The following HTML tags support onunload:

<body>, <frameset>

# #