Execute a script when the document is about to be unloaded in HTML?

HTMLWeb DevelopmentFront End Technology

The onbeforeunload event attribute’ fires when the document is ready to be unloaded.


You can try to run the following code to implement the onbeforeunload attribute −

<!DOCTYPE html>
   <body onbeforeunload = "return display()">
      <p>Close this window or press F5.</p>
         function display() {
            return "Wanna stay here or leave?";
Updated on 03-Mar-2020 12:20:33