revision:
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.)
The onunload attribute is part of the event attributes and can be used on the following element: <body>
<element onunload="script"></element>
script: this attribute contains single value script which works when the onunload event is called.
Close this window or press F5 to reload the page.
Note: Due to different browser settings, this event may not always work as expected.
<body onunload="newFunction()">
<p class="spec">Close this window or press F5 to reload
the page.</p>
<p class="spec"><strong>Note:</strong> Due to different
browser settings, this event may not always work
as expected.</p>
</body>
<script>
function newFunction() {
alert("Thank you for visiting my website!");
}
</script>