title: Block F5 key using JavaScript
body: <p>I have written some code to block the <kbd>F5</kbd> key on web page. It's working fine except when I have to display a large amount of data. If you press the <kbd>F5</kbd> key during the loading duration in which the HTML and JavaScript code is generated, my page gets refreshed.</p>
  <p>Here is the code that I am using to block the <kbd>F5</kbd> key:</p>
  <pre><code>document.onkeydown = fn;
  
  var fn = function (e){ 
      if (!e) 
          var e = window.event;
      var keycode = e.keyCode;    
      if (e.which) 
          keycode = e.which;
      var src = e.srcElement;  
      if (e.target) 
          src = e.target;
      // 116 = F5     
      if (116 == keycode) {
          // Firefox and other non IE browsers
          if (e.preventDefault) { 
              e.preventDefault(); 
              e.stopPropagation();
            // Internet Explorer
          }else if (e.keyCode){ 
              e.keyCode = 0; 
              e.returnValue = false; 
              e.cancelBubble = true; 
          } 
              return false; 
      }
  });
  </code></pre>
  <p>I think this code is not working when the HTML and JavaScript code is generating.</p>