Horje
What is HTML onbeforeunload Event Attribute

Definition and Usage

The onbeforeunload event fires when the document is about to be unloaded.

This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page.

The default message that appears in the confirmation box, is different in different browsers. However, the standard message is something like "Are you sure you want to leave this page?". You cannot remove this message.

However, you can write a custom message together with the default message. See the first example on this page.

Note: In Firefox, only the default message will be displayed (not the custom message (if any)).


Browser Support

The numbers in the table specify the first browser version that fully supports the event.

Syntax

<element onbeforeunload="script">

Attribute Values

Value Description
script The script to be run on onbeforeunload

Technical Details

Supported HTML tags: <body>

Example of HTML onbeforeunload Event

Here executes a JavaScript when the page is about to be unloaded.
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

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




html 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
What is HTML ononline Event Attribute Window Event Attributes
What is HTML onpageshow Event Attribute Window Event Attributes
What is HTML onresize Event Attribute Window Event Attributes

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

Read Full:
Window Event Attributes
Type:
Develop
Category:
Web Tutorial
Sub Category:
Window Event Attributes
Uploaded by:
Admin
Views:
17


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