Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q631 Are you sure you can't simulate charCodeAt() in browsers that don't support it?

You are here: irt.org | FAQ | JavaScript | Text | Q631 [ previous next ]

I'm not, no :) The following was hacked together after an email from Rick Kohler:

<SCRIPT LANGUAGE="JavaScript"><!--
function makeArray() {
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i] = makeArray.arguments[i];
}

var convert = new makeArray('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

function hex(number) {
    var MSD = '' + Math.floor(number / 16), LSD = number - MSD*16;
    if (MSD > 16) return hex(MSD) + convert[LSD]; else return convert[MSD] + convert[LSD];
}

function pHex(number) { return '%' + hex(number); }
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1"><!--
function hex(number) { return number.toString(16); }
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript"><!--
var lookupTable = new Object();
var charCodeTable = new Object();

function buildLookupTable() {
    for (var index = 0; index < 256; index++) {
        lookupTable[unescape(pHex(index))] = index;
        charCodeTable[unescape(pHex(index))] = index;
    }

    if (document.all || document.layers)
        charCodeTable[unescape(pHex(0))] = NaN;
    else
        charCodeTable[unescape(pHex(0))] = 1/0;
    charCodeTable[unescape(pHex(128))] = 8364;
    charCodeTable[unescape(pHex(129))] = 65533;
    charCodeTable[unescape(pHex(130))] = 8218;
    charCodeTable[unescape(pHex(131))] = 402;
    charCodeTable[unescape(pHex(132))] = 8222;
    charCodeTable[unescape(pHex(133))] = 8230;
    charCodeTable[unescape(pHex(134))] = 8224;
    charCodeTable[unescape(pHex(135))] = 8225;
    charCodeTable[unescape(pHex(136))] = 710;
    charCodeTable[unescape(pHex(137))] = 8240;
    charCodeTable[unescape(pHex(138))] = 352;
    charCodeTable[unescape(pHex(139))] = 8249;
    charCodeTable[unescape(pHex(140))] = 338;
    charCodeTable[unescape(pHex(141))] = 65533;
    charCodeTable[unescape(pHex(142))] = 381;
    charCodeTable[unescape(pHex(143))] = 65533;
    charCodeTable[unescape(pHex(144))] = 65533;
    charCodeTable[unescape(pHex(145))] = 8216;
    charCodeTable[unescape(pHex(146))] = 8217;
    charCodeTable[unescape(pHex(147))] = 8220;
    charCodeTable[unescape(pHex(148))] = 8221;
    charCodeTable[unescape(pHex(149))] = 8226;
    charCodeTable[unescape(pHex(150))] = 8211;
    charCodeTable[unescape(pHex(151))] = 8212;
    charCodeTable[unescape(pHex(152))] = 732;
    charCodeTable[unescape(pHex(153))] = 8482;
    charCodeTable[unescape(pHex(154))] = 353;
    charCodeTable[unescape(pHex(155))] = 8250;
    charCodeTable[unescape(pHex(156))] = 339;
    charCodeTable[unescape(pHex(157))] = 65533;
    charCodeTable[unescape(pHex(158))] = 382;
    charCodeTable[unescape(pHex(159))] = 376;
}

buildLookupTable();

function charCode(string) {
    var chr = charCodeTable[string];
    if (string == '0') return chr;
    else if (string == '1') return chr;
    else if (string == '2') return chr;
    else if (string == '3') return chr;
    else if (string == '4') return chr;
    else if (string == '5') return chr;
    else if (string == '6') return chr;
    else if (string == '7') return chr;
    else if (string == '8') return chr;
    else if (string == '9') return chr;
    else if (chr == 48) return 'NaN';
    else if (!(chr < 49) && chr <=57) return chr-48;
    else return chr;
}

var output  = '<TABLE BORDER="1">' +
              '<TR>' +
              '<TH>index<\/TH>' +
              '<TH>hex(index)<\/TH>' +
              '<TH>pHex(index)<\/TH>' +
              '<TH>unescape(pHex(index))<\/TH>' +
              '<TH>escape(unescape(pHex(index)))<\/TH>' +
              '<TH>lookupTable[unescape(pHex(index))]<\/TH>' +
              '<TH>charCode(unescape(pHex(index)))<\/TH>' +
              (document.all || document.layers ? '<TH>unescape(pHex(index)).charCodeAt(0)<\/TH>' : '') +
              '<\/TR>';

for (var index=0; index<256; index++) {
    asciiCode = pHex(index);
    output += '<TR>' +
              '<TD>' + index + '<\/TD>' +
              '<TD>' + hex(index) + '<\/TD>' +
              '<TD>' + pHex(index) + '<\/TD>' +
              '<TD>' + unescape(pHex(index)) + '<\/TD>' +
              '<TD>' + escape(unescape(pHex(index))) + '<\/TD>' +
              '<TD>' + lookupTable[unescape(pHex(index))] + '<\/TD>' +
              '<TD>' + charCode(unescape(pHex(index))) + '<\/TD>' +
              (document.all || document.layers ? '<TD>' + unescape(pHex(index)).charCodeAt(0) + '<\/TD>' : '') +
              '<\/TR>';
}

output     += '<\/TABLE>';

document.write(output);
//--></SCRIPT>

©2018 Martin Webb