Javascript DOM onunload Event

Introduction

Execute a JavaScript when a user unloads the document:

Close this window or press F5 to reload the page.

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

View in separate window

<!DOCTYPE html>
<html>
<body onunload="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {/* w  w w .  j  a va2s.  com*/
  document.getElementById("demo").innerHTML = "Thank you for visiting Examples!";
}
</script>

</body>
</html>

The onunload event occurs once a page has unloaded or the browser window has been closed.

The onunload event is also triggered when a user reloads the page.

In JavaScript:

object.onunload = function(){
       myScript};

In JavaScript, using the addEventListener() method:

object.addEventListener("unload",
       myScript);
Bubbles: No
Cancelable: No
Event type: UiEvent if generated from a user interface, Event otherwise.
Supported HTML tags: <body>



PreviousNext

Related