Horje
HTML onunload Event Attribute
The onunload event attribute in HTML allows the execution of a JavaScript script when a document is being unloaded. This event occurs in various scenarios, including:

Example of HTML onunload Event Attribute

Here executes a JavaScript when a user unloads the document.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body onunload="myFunction()">

<h1>Welcome to my Home Page</h1>

<p>Close this window or press F5 to reload the page.</p>
<p><strong>Note:</strong> Due to different browser settings, this event may not always work as expected.</p>

<script>
function myFunction() {
  alert("Thank you for visiting W3Schools!");
}
</script>

</body>
</html>

Output should be:

Example of HTML onunload Event Attribute

Definition and Usage of HTML onunload Event Attribute

The onunload attribute fires once a page has unloaded (or the browser window has been closed).

onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.)

Note: If you reload a page, you will also trigger the onunload event (and the onload event).

Browser Support of HTML onunload Event Attribute

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

Browser Support of HTML onunload Event Attribute

Syntax of HTML onunload Event Attribute

<element onunload="script">

Attribute Values of HTML onunload Event Attribute

Value Description
script The script to be run on onunload

How to execute a JavaScript when a user unloads the document

Close this window or press F5 to reload the page.

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

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body onunload="myFunction()">

<h1>Welcome to my Home Page</h1>

<p>Close this window or press F5 to reload the page.</p>
<p><strong>Note:</strong> Due to different browser settings, this event may not always work as expected.</p>

<script>
function myFunction() {
  alert("Thank you for visiting W3Schools!");
}
</script>

</body>
</html>

Output should be:

How to execute a JavaScript when a user unloads the document




html event attributes

Related Articles
HTML onafterprint Event Attribute Window Event Attributes
HTML onbeforeprint Event Attribute Window Event Attributes
HTML onbeforeunload Event Attribute Window Event Attributes
HTML onerror Event Attribute Window Event Attributes
HTML onhashchange Event Attribute Window Event Attributes
HTML onload Event Attribute Window Event Attributes
HTML onoffline Event Attribute Window Event Attributes
HTML ononline Event Attribute Window Event Attributes
HTML onpageshow Event Attribute Window Event Attributes
HTML onresize Event Attribute Window Event Attributes
HTML onunload Event Attribute Window Event Attributes

Single Articles
Example of HTML onunload Event AttributeWindow Event Attributes
Definition and Usage of HTML onunload Event AttributeWindow Event Attributes
Browser Support of HTML onunload Event AttributeWindow Event Attributes
Syntax of HTML onunload Event AttributeWindow Event Attributes
Attribute Values of HTML onunload Event AttributeWindow Event Attributes
How to execute a JavaScript when a user unloads the documentWindow Event Attributes

Type:
Develop
Category:
Web Tutorial
Sub Category:
Window Event Attributes
Uploaded by:
Admin


Reffered: https://www.w3schools.com/tags/ev_onunload.asp