Prevent Browser from refreshing when keypress is handled - Javascript DOM

Javascript examples for DOM:Key Event

Description

Prevent Browser from refreshing when keypress is handled

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script>
function preventStuff () {/*from   w ww.  j  a  va  2s.c o  m*/
   var e = window.event;
   if(e.ctrlKey) {
     switch(e.keyCode) {
     case 82 :
       document.body.innerHTML += "<p>Window refresh prevented.</p>";
       e.returnValue = false;
       break;
     case 72 :
       document.body.innerHTML += "<p>Browser history prevented.</p>";
       e.returnValue = false;
       break;
     }
   }
}

      </script> 
   </head> 
   <body onkeydown="preventStuff()">  
   </body>
</html>

Related Tutorials