What is the usage of onunload event in JavaScript?


The unload event trigger when you unload (or refresh) a page. You can try to run the following code to learn how to implement onunload event in JavaScript.

Example

<!DOCTYPE html>
<html>
   <body onunload="newFunc()">
      <p>Close the page and see what happens!</p>
      <p>This event may give unexpected results.</p>
      <script>
         function newFunc() {
            alert("Thank you!");
         }
      </script>
   </body>
</html>

Updated on: 21-May-2020

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements