Home > Javascript > charCode is meaningless

charCode is meaningless

I have seen this Javascript error in my Firefox Error Console many times, from many high profile sites about the internet.

Warning: The ‘charCode’ property of a keyup event should not be used. The value is meaningless.

I’m trying not to be smug, but it does feel good to know that List Central uses the charCode concept while not throwing any Javascript errors. I’ve aimed to keep that Error Console clean through the entire development of List Central.

For those of you that want to get rid of that pesky charCode error, change

var code = event.charCode;

to:

var code;
if (e.keyCode){
    code = e.keyCode;
}else if (e.which) {
    code = e.which;
}
var character = String.fromCharCode(code).toLowerCase();
Share and Enjoy:
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • StumbleUpon
  • Reddit
  • Technorati
  • NewsVine
  • RSS

You might also like...

Categories: Javascript Tags: ,
  1. February 9th, 2010 at 17:03 | #1

    hi marilyn
    I read your charCode warning fix. however it doesnt seem to work for me. I get this error coz of following function

    function encode64(input) {
    var keyStr = “ABCDEFGHIJKLMNOP” +
    “QRSTUVWXYZabcdef” +
    “ghijklmnopqrstuv” +
    “wxyz0123456789+/” +
    “=”;
    input = escape(input);
    var output = “”;
    var chr1, chr2, chr3 = “”;
    var enc1, enc2, enc3, enc4 = “”;
    var i = 0;

    do {
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);

    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) <> 4);
    enc3 = ((chr2 & 15) <> 6);
    enc4 = chr3 & 63;

    if (isNaN(chr2)) {
    enc3 = enc4 = 64;
    } else if (isNaN(chr3)) {
    enc4 = 64;
    }

    output = output +
    keyStr.charAt(enc1) +
    keyStr.charAt(enc2) +
    keyStr.charAt(enc3) +
    keyStr.charAt(enc4);
    chr1 = chr2 = chr3 = “”;
    enc1 = enc2 = enc3 = enc4 = “”;
    } while (i < input.length);

    return output;
    }

    IT HAS charcode

  2. February 9th, 2010 at 19:16 | #2

    Hi Charu. I’ve set up your function over here and am not getting any charCode error. You code does give and error with the lines:

    enc2 = ((chr1 & 3) <> 4);
    enc3 = ((chr2 & 15) <> 6)

    because of the <>. I’m not sure what you are trying to accomplish, nor how to help you. Do you have a site running that is throwing the charCode warning that I could have a look at?

  3. charu verma
    February 11th, 2010 at 10:13 | #3

    first of all thanks a lot for replying. and this is encode64 function being used to encode a string in URL. right now this site is under development, so havent been launched yet. so how shud i fix the lines u mentionaed? coz this code is successfully generating 64 bit encoded string. but at the same time charcode warning!, but i am not sure its coz of this function!

    • February 11th, 2010 at 13:56 | #4

      Hi Charu. I suspect that the warning is coming from elsewhere on your site, as this function doesn’t give the error when I use it. Look in your code for charCode, not charCodeAt for the offending warning. Good luck!

  4. Diego
    December 15th, 2010 at 14:07 | #5

    a good solution.. for this
    try change charCode -> XcharCode in source jquery-1.X.X.js and everything works OK

    in the lastest version 1.4.4 there are “4″ ocurrences…

  1. No trackbacks yet.