When is the page left/unloaded, handle load, unload and before unload event - Javascript DOM

Javascript examples for DOM:Event

Description

When is the page left/unloaded, handle load, unload and before unload event

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
function testLoad() {/*  w  ww  .j  av a 2  s .co m*/
  console.log('loaded');
}
function testBeforeUnload() {
  return "test before unload event";
  //console.log("test");
}
function testUnload() {
  console.log("test unload event");
}

      </script> 
   </head> 
   <body onload="testLoad()" onunload="testUnload()" onbeforeunload="return testBeforeUnload()">    
   </body>
</html>

Related Tutorials