How to make a Javascript shortcut for all browsers

I am trying to make a shortcut with javascript. It works with FF, but not IE8. I am using this code -

document.onkeydown=function(e)
{ 

if(e.which == 83) 

{ alert("hello"); } 
}

      

Please give me a simple code that will support all browsers. Thanks to

+2


source to share


3 answers


To make your own cross-browser you need:

document.onkeydown = function(e) { 
  e = e || window.event;
  var keyCode = e.keyCode || e.which;

  if(keyCode == 83) { alert("hello"); }
}

      



Check out the above snippet here .

+3


source


Read this.



+2


source


Are you allowed to use jQuery? Because this will work:

$(window).keydown(function(event){
     if(event.keyCode == 83){
          alert('hello');
     } 
});

      

Partially strengthened from here

0


source







All Articles