Horje

How to execute a JavaScript when the page is about to be unloaded

Close this window, press F5 or click on the link below to invoke the onbeforeunload event.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">

<p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p>

<a href="https://horje.com">Click here to go to horje.com</a>
  
<script>
function myFunction() {
  return "Write something clever here...";
}
</script>

</body>
</html>

Output should be:

How to execute a JavaScript when the page is about to be unloaded



Single Articles
How to execute a JavaScript when the page is about to be unloadedWindow Event Attributes


Related Articles
What is HTML onafterprint Event Attribute Window Event Attributes
What is HTML onbeforeprint Event Attribute Window Event Attributes
What is HTML onbeforeunload Event Attribute Window Event Attributes
What is HTML onerror Event Attribute Window Event Attributes
What is HTML onhashchange Event Attribute Window Event Attributes
What is HTML onload Event Attribute Window Event Attributes
What is HTML onoffline Event Attribute Window Event Attributes

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