Add event handler to window with addEventListener method - Javascript DOM

Javascript examples for DOM:Element addEventListener

Description

Add event handler to window with addEventListener method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from w  w  w  .  jav  a 2 s. c  o  m
    window.addEventListener("beforeunload", function (evt) {
      evt.returnValue = "my message";
    }, false);
    }

      </script> 
   </head> 
   <body> 
      <a href="http://java2s.com">website</a>  
   </body>
</html>

Related Tutorials