What is the usage of the onbeforeunload event in JavaScript?


If you want to trigger an event before the document is loaded, then use the onbeforeunload event.

Example

You can try to run the following code to learn how to implement onbeforeunload event in JavaScript.

<!DOCTYPE html>
<html>
   <body onbeforeunload="return myFunction()">

      <a href="https://www.qries.com">Click to open Qries</a>
      <script>
         function myFunction() {
            return "Working with onbeforeunload event";
         }
      </script>
   </body>
</html>

Updated on: 21-May-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements