2010-11-19 91 views
1

我使用下面的函数来编码字符串到的base64:charCodeAt在IE中不支持?

encode: function(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) | (chr2 >> 4); 
     enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 
     enc4 = chr3 & 63; 

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

     output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4); 
    } while (i < input.length); 

    return output; 
} 

然而,在IE中,我得到了以下错误:

chr1 = input.charCodeAt(i++); 
"Object doesn't supported by this property or method". 

能否请你帮我解决这个问题?看来charCodeAt函数不起作用。

谢谢你的时间。

+2

哪个版本的IE? – 2010-11-19 04:59:19

+5

IE 5.5+支持'charCodeAt()'。你还支持 Alex 2010-11-19 05:00:28

回答

1

它肯定看起来像 IE支持String#charCodeAt()w3schoolsMSDN

什么是typeof input

+0

我发现类型是“数字”,所以我添加了一个检查,如输入类型不是字符串,它调用toString()函数来转换它。 – Benjamin 2010-11-22 01:46:52

0

如果你真的在< IE 5.5,则必须做到这一点:

input.charAt(i++).escape() 

会做你想要什么。

但是,这在unicode字符上失败,但在IE 5.4及以下的黑暗日子里,unicode几乎不存在。

如果您正在使用> = IE 5.5,那么问题不在于charCodeAt(),而是与您的代码的另一部分。什么是input