w3resource

HTML onunload attribute

onunload

The purpose of the HTML onunload attribute is to execute a script when the associated elements unload.

Supported elements

HTML onunload attribute supports body and frameset elements.

Syntax

<ElementName onunload="value" >.....</ElementName>

Where ElementName is any supported element.

Type of value

Script.

Value

A script which is to be executed.

Default value

There is no default value of HTML onunload attribute.

Supported doctypes

When used with body element HTML 4.01 strict, HTML 4.01 transitional.

When used with frameset element HTML 4.01 frameset.

Example of HTML onunload attribute with body element

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example of HTML onunload attribute with body element</title>
</head>
<body onunload="alert('Thanks for visiting')" >
<p>We are learning HTML onunload element.</p> 
</body>
</html>

Result

html onunload attribute with body element

View this example in a separate browser window

Example of HTML onunload attribute with body element

Example of HTML onunload attribute with frameset element

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example of HTML onunload attribute with frameset element</title>
</head>
<frameset rows="20%,*,20%" onunload="alert('Thanks for visiting')" > 
<frame src="header.html" frameborder="1"  name="header" title="Header window"> 
<frame src="content.html" frameborder="0"  name="content" title="Content window" marginheight="10">
<frame src="footer.html" frameborder="0"  name="footer" title="footer window" marginheight="10">
</frameset>
</html>

Result

html onunload attribute with frameset element

View this example in a separate browser window

Example of HTML onunload attribute with frameset element

Previous: HTML onsubmit attribute
Next: HTML profile attribute