views:

913

answers:

2

Firebug 1.5.4 JavaScript warning : The 'charCode' property of akeyupevent should not be used. The value is meaningless. To ignore it? Is there any issue?

The warning appears to jQuery 1.4.2 keyupandkeydown, not onkeypress.
I have read that on changingevent.keyCodeandevent.charCodetoevent.whichmust fix it, but it does not work for me.
Full code example in http://jsfiddle.net/zTevK/2/ and in question
My code useskeyupand does not work withkeypress.

  $(document).bind('keyup', function(e){
   var key = e.which;
   if (key > 36 && key < 41) {
    if (key == 37) { changeTab(-1); }
    if (key == 38) { changeTab(-imgPerRow); }
    if (key == 39) { changeTab(+1); }
    if (key == 40) { changeTab(+imgPerRow); }
    e.preventDefault();
  ...
+1  A: 

This is a very common error message that comes out of mozilla code. The message comes out for every keystroke and it can lead to performance problems if you have the error console open. I've complained to mozilla about this and similar messages to no effect. jjb

johnjbarton
It's a useful warning. Anything that helps ease the confusion in developers' minds between the roles of `keypress` versus `keydown` and `keyup` events is a good thing in my book.
Tim Down
+3  A: 

The jQuery code itself normalizes every event in jQuery.event.fix

// props includes 'charCode' - this will access it
for ( var i = this.props.length, prop; i; ) {
  prop = this.props[ --i ];
  event[ prop ] = originalEvent[ prop ];
}

// also, later in the same function

// Add which for key events
if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) {
  event.which = event.charCode || event.keyCode;
}

One of these lines of code is going to access charCode, which in turn creates your warning... You don't even need to do anything in your event handler (illustrated on jsfiddle)...

The "solution" I usually end up using is just running without JS warnings (errors still show up) Turning off Warnings

You can safely ignore this message (assuming you aren't using charCode, and are indeed using which)

gnarf
So Firebug warning is wrong?
Binyamin
Not wrong, its warning is accurate, jQuery checks the charCode property itself when deciding to use charCode or keyCode, and when normalizing its event. The warning is safe to ignore if you are always using `e.which` though.
gnarf
@Binyamin: jQuery is wrong. It shouldn't be doing anything with `charCode` for `keydown` or `keyup` events.
Tim Down