Welcome to WebmasterWorld Guest from 54.234.153.186

Forum Moderators: open

Message Too Old, No Replies

event.keycode Firefox

event.keycode does not work in Firefox

     

jaruba

7:14 am on Oct 27, 2011 (gmt 0)

5+ Year Member



Now I've been banging my head against the wall with this for a while now.. say I have this script:

<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck()
{
var KeyID = event.keyCode;
switch(KeyID)
{
case 13:
alert("hello");
}
}
</script>


The code should make an alert box with the text "hello" when someone presses the Enter key.
Why doesn't this work in Firefox? (tested in Chrome, Opera and IE, all work fine)

birdbrain

10:19 am on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there jaruba,

try it like this...

<script type="text/javascript">
document.onkeyup=KeyCheck;
function KeyCheck(evt) {
var evt=(evt)?evt:event;
var KeyID = evt.keyCode;
switch(KeyID) {
case 13:
alert("hello");
break;
default:
alert("not 13");
}
}
</script>

birdbrain

jaruba

10:27 am on Oct 27, 2011 (gmt 0)

5+ Year Member



You, my friend, are a titan among humble humans.. :))

It works as expected in IE, Firefox, Opera and Chrome. Thank you.

Fotiman

1:06 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The reason, fyi, is that event handling is done differently in IE than other browsers. IE has a single global event object, while other browsers pass the event object to the handler.

birdbrain

2:43 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi here jaruba,

You, my friend, are a titan among humble humans...

That is really weird. :)

I awoke this morning as just another "bald headed old fart", then typed
a few characters on my keyboard and became a Greek god before lunch.

birdbrain
 

Featured Threads

Hot Threads This Week

Hot Threads This Month