JavaScriptティプス

ブラウザの判別

if (document.getElementById)
{
  // DOM対応
}

if (document.implementation.hasFeature('Core', '2.0'))
{
  // DOM 2対応
}

if (document.layers)
{
  // Netscape 4
}

if (document.all && !document.getElementById)
{
  // Internet Explorer 4
}

if (document.all)
{
  // Internet Explorer 4以降
}

if (typeof document.documentElement.style.msInterpolationMode != 'undefined')
{
  // Internet Explorer 7
}

Internet Explorerのバージョン等は条件コンパイルを使ってもっと詳細に得られる:

/*@cc_on @*/
/*@if (@_jscript_version >= 4)
   alert("JScriptバージョン4以降です。");
   @else @*/
   alert("JScriptバージョン3以前です。");
/*@end @*/

またInternet Explorerかそうでないかは次の一行で判別できる:

var isMSIE = /*@cc_on!@*/false;

文字列のキャメルケース化

foo-bar-bazのような文字列をfooBarBazという文字列に変換する関数:

function camelize(s)
{
  return s.replace(/[^\w]+([\w])?/g, function(match, s)
  {
    return s ? s.toUpperCase() : '';
  });
}

CRC16チェックサムの計算

String.prototype.crc16 = function ()
{
  var table = new Array(256);
  for (var i = 0; i < 256; ++i)
  {
    var c = i;
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0x8408 ^ (c >>> 1)) : (c >>> 1);
    table[i] = c;
  }

  var crc = 0xFFFF;
  for (var i = 0, j = this.length; i < j; ++i)
  {
    var n = this.charCodeAt(i);
    var a = new Array;
    do
    {
      a[a.length] = n & 0xFF;
    }
    while (n >>>= 8) ;
    for (var k = 0, l = a.length; k < l; ++k)
    {
      crc = table[(crc ^ a[k]) & 0xFF] ^ (crc >>> 8);
    }
  }
  return crc;
}

CRC32チェックサムの計算

String.prototype.crc32 = function ()
{
  var table = new Array(256);
  for (var i = 0; i < 256; ++i)
  {
    var c = i;
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
    table[i] = c;
  }

  var crc = 0xFFFFFFFF;
  for (var i = 0, j = this.length; i < j; ++i)
  {
    var n = this.charCodeAt(i);
    var a = new Array;
    do
    {
      a[a.length] = n & 0xFF;
    }
    while (n >>>= 8) ;
    for (var k = 0, l = a.length; k < l; ++k)
    {
      crc = table[(crc ^ a[k]) & 0xFF] ^ (crc >>> 8);
    }
  }
  crc = ~crc;
  return crc;
}

Adler32チェックサムの計算

String.prototype.adler32 = function ()
{
  var a = 0x00000000;
  var b = 0x00000001;
  for (var i = 0, j = this.length; i < j; ++i)
  {
    var x = this.charCodeAt(i);
    var y = x & 0x000000FF;
    var z = (x & 0x0000FF00) >> 8;
    b = (b + y) % 0x0000FFF1;
    a = (a + b) % 0x0000FFF1;
    b = (b + z) % 0x0000FFF1;
    a = (a + b) % 0x0000FFF1;
  }
  return (a << 16) + b;
}

MD5ハッシュの計算

String.prototype.md5 = function ()
{
  var sz = 8;  // bits per input character.  8 - ASCII; 16 - Unicode
  var table = '0123456789ABCDEF';

  function add(x, y)
  {
    var lsw = (x & 0xFFFF) + (y & 0xFFFF);
    var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    return (msw << 16) | (lsw & 0xFFFF);
  }

  function rol(num, cnt)
  {
    return (num << cnt) | (num >>> (32 - cnt));
  }

  function FF(a, b, c, d, x, s, t)
  {
    return add(rol(add(add(a, (b & c) | (~b & d)), add(x, t)), s), b);
  }

  function GG(a, b, c, d, x, s, t)
  {
    return add(rol(add(add(a, (b & d) | (c & ~d)), add(x, t)), s), b);
  }

  function HH(a, b, c, d, x, s, t)
  {
    return add(rol(add(add(a, b ^ c ^ d), add(x, t)), s), b);
  }

  function II(a, b, c, d, x, s, t)
  {
    return add(rol(add(add(a, c ^ (b | ~d)), add(x, t)), s), b);
  }

  var len = this.length * sz;

  var x = new Array((len >> 5) + 1).fill(0);
  var mask = (1 << sz) - 1;

  for (var i = 0; i < len; i += sz)
  {
    x[i >> 5] |= (this.charCodeAt(i / sz) & mask) << (i % 32);
  }

  x[len >> 5] |= 0x80 << (len % 32);
  x[((len + 64 >>> 9) << 4) + 14] = len;

  var a = 1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d = 271733878;

  for (var i = 0, j = x.length; i < j; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = FF(a, b, c, d, x[i], 7, -680876936);
    d = FF(d, a, b, c, x[i + 1] || 0, 12, -389564586);
    c = FF(c, d, a, b, x[i + 2] || 0, 17, 606105819);
    b = FF(b, c, d, a, x[i + 3] || 0, 22, -1044525330);
    a = FF(a, b, c, d, x[i + 4] || 0, 7, -176418897);
    d = FF(d, a, b, c, x[i + 5] || 0, 12, 1200080426);
    c = FF(c, d, a, b, x[i + 6] || 0, 17, -1473231341);
    b = FF(b, c, d, a, x[i + 7] || 0, 22, -45705983);
    a = FF(a, b, c, d, x[i + 8] || 0, 7, 1770035416);
    d = FF(d, a, b, c, x[i + 9] || 0, 12, -1958414417);
    c = FF(c, d, a, b, x[i + 10] || 0, 17, -42063);
    b = FF(b, c, d, a, x[i + 11] || 0, 22, -1990404162);
    a = FF(a, b, c, d, x[i + 12] || 0, 7, 1804603682);
    d = FF(d, a, b, c, x[i + 13] || 0, 12, -40341101);
    c = FF(c, d, a, b, x[i + 14] || 0, 17, -1502002290);
    b = FF(b, c, d, a, x[i + 15] || 0, 22, 1236535329);

    a = GG(a, b, c, d, x[i + 1] || 0, 5, -165796510);
    d = GG(d, a, b, c, x[i + 6] || 0, 9, -1069501632);
    c = GG(c, d, a, b, x[i + 11] || 0, 14, 643717713);
    b = GG(b, c, d, a, x[i], 20, -373897302);
    a = GG(a, b, c, d, x[i + 5] || 0, 5, -701558691);
    d = GG(d, a, b, c, x[i + 10] || 0, 9, 38016083);
    c = GG(c, d, a, b, x[i + 15] || 0, 14, -660478335);
    b = GG(b, c, d, a, x[i + 4] || 0, 20, -405537848);
    a = GG(a, b, c, d, x[i + 9] || 0, 5, 568446438);
    d = GG(d, a, b, c, x[i + 14] || 0, 9, -1019803690);
    c = GG(c, d, a, b, x[i + 3] || 0, 14, -187363961);
    b = GG(b, c, d, a, x[i + 8] || 0, 20, 1163531501);
    a = GG(a, b, c, d, x[i + 13] || 0, 5, -1444681467);
    d = GG(d, a, b, c, x[i + 2] || 0, 9, -51403784);
    c = GG(c, d, a, b, x[i + 7] || 0, 14, 1735328473);
    b = GG(b, c, d, a, x[i + 12] || 0, 20, -1926607734);

    a = HH(a, b, c, d, x[i + 5] || 0, 4, -378558);
    d = HH(d, a, b, c, x[i + 8] || 0, 11, -2022574463);
    c = HH(c, d, a, b, x[i + 11] || 0, 16, 1839030562);
    b = HH(b, c, d, a, x[i + 14] || 0, 23, -35309556);
    a = HH(a, b, c, d, x[i + 1] || 0, 4, -1530992060);
    d = HH(d, a, b, c, x[i + 4] || 0, 11, 1272893353);
    c = HH(c, d, a, b, x[i + 7] || 0, 16, -155497632);
    b = HH(b, c, d, a, x[i + 10] || 0, 23, -1094730640);
    a = HH(a, b, c, d, x[i + 13] || 0, 4, 681279174);
    d = HH(d, a, b, c, x[i], 11, -358537222);
    c = HH(c, d, a, b, x[i + 3] || 0, 16, -722521979);
    b = HH(b, c, d, a, x[i + 6] || 0, 23, 76029189);
    a = HH(a, b, c, d, x[i + 9] || 0, 4, -640364487);
    d = HH(d, a, b, c, x[i + 12] || 0, 11, -421815835);
    c = HH(c, d, a, b, x[i + 15] || 0, 16, 530742520);
    b = HH(b, c, d, a, x[i + 2] || 0, 23, -995338651);

    a = II(a, b, c, d, x[i], 6, -198630844);
    d = II(d, a, b, c, x[i + 7] || 0, 10, 1126891415);
    c = II(c, d, a, b, x[i + 14] || 0, 15, -1416354905);
    b = II(b, c, d, a, x[i + 5] || 0, 21, -57434055);
    a = II(a, b, c, d, x[i + 12] || 0, 6, 1700485571);
    d = II(d, a, b, c, x[i + 3] || 0, 10, -1894986606);
    c = II(c, d, a, b, x[i + 10] || 0, 15, -1051523);
    b = II(b, c, d, a, x[i + 1] || 0, 21, -2054922799);
    a = II(a, b, c, d, x[i + 8] || 0, 6, 1873313359);
    d = II(d, a, b, c, x[i + 15] || 0, 10, -30611744);
    c = II(c, d, a, b, x[i + 6] || 0, 15, -1560198380);
    b = II(b, c, d, a, x[i + 13] || 0, 21, 1309151649);
    a = II(a, b, c, d, x[i + 4] || 0, 6, -145523070);
    d = II(d, a, b, c, x[i + 11] || 0, 10, -1120210379);
    c = II(c, d, a, b, x[i + 2] || 0, 15, 718787259);
    b = II(b, c, d, a, x[i + 9] || 0, 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }

  var x = [a, b, c, d];
  var result = '';

  for (var i = 0; i < 16; ++i)
  {
    result += table.charAt((x[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF)
            + table.charAt((x[i >> 2] >> ((i % 4) * 8)) & 0xF);
  }

  return result;
}

SHA1ハッシュの計算

String.prototype.sha1 = function ()
{
  var sz = 8;  // bits per input character.  8 - ASCII; 16 - Unicode
  var table = '0123456789ABCDEF';

  function add(x, y)
  {
    var lsw = (x & 0xFFFF) + (y & 0xFFFF);
    var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    return (msw << 16) | (lsw & 0xFFFF);
  }

  function rol(num, cnt)
  {
    return (num << cnt) | (num >>> (32 - cnt));
  }

  var len = this.length * sz;

  var x = new Array((len >> 5) + 1).fill(0);
  var mask = (1 << sz) - 1;

  for (var i = 0; i < len; i += sz)
  {
    x[i >> 5] |= (this.charCodeAt(i / sz) & mask) << (32 - sz - i % 32);
  }

  x[len >> 5] |= 0x80 << (24 - len % 32);
  x[((len + 64 >> 9) << 4) + 15] = len;

  var w = Array(80);
  var a = 1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d = 271733878;
  var e = -1009589776;

  for (var i = 0, j = x.length; i < j; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;
    var olde = e;

    for (var k = 0; k < 80; ++k)
    {
      if (k < 16)
      {
        w[k] = x[i + k];
      }
      else
      {
        w[k] = rol(w[k - 3] ^ w[k - 8] ^ w[k - 14] ^ w[k - 16], 1);
      }

      var ft = k < 20 ? (b & c) | (~b & d)
                      : k < 40 ? b ^ c ^ d
                               : k < 60 ? (b & c) | (b & d) | (c & d)
                                        : b ^ c ^ d;

      var kt = k < 20 ? 1518500249
                      : k < 40 ? 1859775393
                               : k < 60 ? -1894007588
                                        : -899497514;

      var f = add(add(rol(a, 5), ft), add(add(e, w[k]), kt));

      e = d;
      d = c;
      c = rol(b, 30);
      b = a;
      a = f;
    }

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
    e = add(e, olde);
  }

  var x = [a, b, c, d, e];
  var result = '';

  for (var i = 0; i < 20; ++i)
  {
    result += table.charAt((x[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF)
            + table.charAt((x[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
  }

  return result;
}

MIME base64エンコード/デコード

String.prototype.base64_encode = function ()
{
  var table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  var result = '';
  var i = 0;

  while (i < this.length)
  {
    var x = this.charCodeAt(i++);
    var y = this.charCodeAt(i++);
    var z = this.charCodeAt(i++);

    var a = x >> 2;
    var b = ((x & 3) << 4) | (y >> 4);
    var c = ((y & 15) << 2) | (z >> 6);
    var d = z & 63;

    if (isNaN(y))
    {
      c = d = 64;
    }
    else if (isNaN(z))
    {
      d = 64;
    }

    result += table.charAt(a) + table.charAt(b)
            + table.charAt(c) + table.charAt(d);
  }

  return result;
}

String.prototype.base64_decode = function ()
{
  var table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  var result = '';
  var i = 0;

  while (i < this.length)
  {
    var a = table.indexOf(this.charAt(i++));
    var b = table.indexOf(this.charAt(i++));
    var c = table.indexOf(this.charAt(i++));
    var d = table.indexOf(this.charAt(i++));

    var x = (a << 2) | (b >> 4);
    var y = ((b & 15) << 4) | (c >> 2);
    var z = ((c & 3) << 6) | d;

    result += String.fromCharCode(x);

    if (c != 64)
    {
      result += String.fromCharCode(y);
    }

    if (d != 64)
    {
      result += String.fromCharCode(z);
    }
  }

  return result;
}