XHTML » body » onunload

Syntax:
onunload="action"

The onunload event occurs just before the HTML document is unloaded or removed from viewing (also when the page is refreshed). When this event happens, the code calls a function that performs a desired action. For example, you may want an alert box to appear that provides useful information to the user.

It can be used with either the body or frameset tags.
 
In contrast, the onload event occurs just after the HTML document is loaded for viewing.

Examples

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>DevGuru XHTML onunload Tag Example</title>
</head>
<body>
<body onunload="JavaScript:alert('Thank you for\nvisiting DevGuru!')">
Please exit or refresh this page to see output.
<br />
The alert box should appear.
</body>
</html>
Explanation:

In this example, the onunload event causes an alert box with a message to appear when you exit the page.

Language(s): XHTML