homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
event.keycode Firefox
event.keycode does not work in Firefox
jaruba

5+ Year Member



 
Msg#: 4380023 posted 7:14 am on Oct 27, 2011 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4380023 posted 10:19 am on Oct 27, 2011 (gmt 0)

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

5+ Year Member



 
Msg#: 4380023 posted 10:27 am on Oct 27, 2011 (gmt 0)

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

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

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4380023 posted 1:06 pm on Oct 27, 2011 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4380023 posted 2:43 pm on Oct 27, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved