Horje
HTML onunload Attribute

Definition and Usage

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).


Applies to

The onunload attribute is part of the Event Attributes, and can be used on the following element:

Elements Event
<body> onunload

Browser Support


How to Execute a JavaScript when a user unloads the document - HTML onunload Attribute

Click on F5 button to reload the page.
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 onunload Attribute




html onunload attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Attribute
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference