homepage Welcome to WebmasterWorld Guest from 107.22.70.215
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

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




msg:4380025
 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




msg:4380093
 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




msg:4380096
 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




msg:4380145
 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




msg:4380196
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved