views:

202

answers:

3

I'm trying to make shortcut with javascript. It works with FF but not with IE8. I'm using this code -

document.onkeydown=function(e)
{ 

if(e.which == 83) 

{ alert("hello"); } 
}

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

+2  A: 

Read this.

bcat
Thank you for the link. really very helpful. It will help me to learn more about them. I really like to learn something new.
SHAKTI
A: 

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

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

Partially boosted from here

inkedmn
Thank you, But I don't like to use jQuery.
SHAKTI
+2  A: 

In order to make your code cross-browser, you should:

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

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

Check the above snippet here.

CMS
It works. I was really looking something like that.Thank you very very much. :-)
SHAKTI
You're welcome SHAKTI.
CMS