我想使用atob將base64編碼數據轉換爲字符串。我發現它在Mozilla和Chrome中工作正常,但它在IE中不起作用。請告訴我這方面的任何substitue,或如何讓IEatob在IE中不工作
回答
走這工作,此鏈接,包括JS目前有:
http://code.google.com/p/stringencoders/source/browse/trunk/javascript/base64.js?r=230
/*
* Copyright (c) 2010 Nick Galbreath
* See full license on http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
*/
var base64 = {};
base64.PADCHAR = '=';
base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/';
base64.makeDOMException = function() {
// sadly in FF,Safari,Chrome you can't make a DOMException
var e, tmp;
try {
return new DOMException(DOMException.INVALID_CHARACTER_ERR);
} catch (tmp) {
// not available, just passback a duck-typed equiv
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype
var ex = new Error("DOM Exception 5");
// ex.number and ex.description is IE-specific.
ex.code = ex.number = 5;
ex.name = ex.description = "INVALID_CHARACTER_ERR";
// Safari/Chrome output format
ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };
return ex;
}
}
base64.getbyte64 = function(s,i) {
// This is oddly fast, except on Chrome/V8.
// Minimal or no improvement in performance by using a
// object with properties mapping chars to value (eg. 'A': 0)
var idx = base64.ALPHA.indexOf(s.charAt(i));
if (idx === -1) {
throw base64.makeDOMException();
}
return idx;
}
base64.decode = function(s) {
// convert to string
s = '' + s;
var getbyte64 = base64.getbyte64;
var pads, i, b10;
var imax = s.length
if (imax === 0) {
return s;
}
if (imax % 4 !== 0) {
throw base64.makeDOMException();
}
pads = 0
if (s.charAt(imax - 1) === base64.PADCHAR) {
pads = 1;
if (s.charAt(imax - 2) === base64.PADCHAR) {
pads = 2;
}
// either way, we want to ignore this last block
imax -= 4;
}
var x = [];
for (i = 0; i < imax; i += 4) {
b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |
(getbyte64(s,i+2) << 6) | getbyte64(s,i+3);
x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));
}
switch (pads) {
case 1:
b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);
x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));
break;
case 2:
b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);
x.push(String.fromCharCode(b10 >> 16));
break;
}
return x.join('');
}
base64.getbyte = function(s,i) {
var x = s.charCodeAt(i);
if (x > 255) {
throw base64.makeDOMException();
}
return x;
}
base64.encode = function(s) {
if (arguments.length !== 1) {
throw new SyntaxError("Not enough arguments");
}
var padchar = base64.PADCHAR;
var alpha = base64.ALPHA;
var getbyte = base64.getbyte;
var i, b10;
var x = [];
// convert to string
s = '' + s;
var imax = s.length - s.length % 3;
if (s.length === 0) {
return s;
}
for (i = 0; i < imax; i += 3) {
b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);
x.push(alpha.charAt(b10 >> 18));
x.push(alpha.charAt((b10 >> 12) & 0x3F));
x.push(alpha.charAt((b10 >> 6) & 0x3f));
x.push(alpha.charAt(b10 & 0x3f));
}
switch (s.length - imax) {
case 1:
b10 = getbyte(s,i) << 16;
x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
padchar + padchar);
break;
case 2:
b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);
x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
alpha.charAt((b10 >> 6) & 0x3f) + padchar);
break;
}
return x.join('');
}
如何使用這個js for iframe src。 – 2013-09-05 12:33:19
這個庫是不是在某些情況下可靠的,例如,它恐慌此字符串:直接http://kjur.github.io/jsrsasign/ 或者使用這一個: eyJhY3QiOiJpZG0sZmFjZWJvb2siLCJjb21maXJtZWQiOmZhbHNlLCJleHAiOjE0NTcwMDAwNjYsInN1YiI6ImJvbS5kLnZhbkBnbWFpbC5jb20ifQ – Van 2016-02-03 01:54:54
推薦這個庫的http:// kjur.github.io/jsrsasign/api/symbols/src/base64x-1.1.js.html – Van 2016-02-03 01:56:53
ATOB和BTOA是Mozilla特定功能之後該支持已擴展到webkit瀏覽器,如chrome,safari,因此它在IE中不起作用。
所以最好使用一些其他的功能,如: -
/**
*
* Base64 encode/decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
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);
}
return output;
},
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
這將解決您的問題
如果我的輸入字符串具有必須進行編碼的管道符號(|),則這不起作用。 Chrome和IE爲該輸入字符串和管道提供了不同的編碼結果。 – 2013-09-11 07:49:20
@Kanagavelu Sugumar,你解決了你的問題嗎? – 2013-11-15 20:10:27
__ @ Kanagavelu Sugumar__,你解決了你的問題嗎? – Redsandro 2014-02-11 20:54:34
- 1. Window.focus()在IE中不工作
- 2. 在IE中不工作「onchange」
- 3. Colorbox在IE中不工作
- 4. jquery不在IE中工作
- 5. BlockUi在IE中不工作
- 6. document.execCommand()在IE中不工作
- 7. $ .load()在ie中不工作
- 8. jqBootstrapValidation.js在IE中不工作
- 9. JQuery不在IE中工作
- 10. ELEMENT.style.color在IE中不工作
- 11. Cookie在IE中不工作
- 12. CSS在IE中不工作
- 13. FB.XFBML.parse在IE中不工作
- 14. .each()在IE中不工作
- 15. $ .post()在IE中不工作
- 16. 。不在IE中工作
- 17. $ route.reload()在IE中不工作
- 18. Window.Location在IE中不工作?
- 19. Flexslider在IE中不工作
- 20. CSS3PIE在IE中不工作
- 21. .innerHTML在IE中不工作
- 22. document.cookie在IE中不工作
- 23. Jplayer不在IE中工作
- 24. solr在IE中不工作
- 25. .sort在IE中不工作(IE 9)
- 26. atob()在Safari中的例外,但在Chrome中工作
- 27. css在ie中不工作,儘管ie css工作表
- 28. jquery不工作ie ie
- 29. JavaScript不工作在IE 8
- 30. jQuery的不工作在IE
試試這個:http://code.google.com/p/stringencoders/source/browse/trunk /javascript/base64.js?r=230 – 2012-07-17 14:10:13
微軟終於在Internet Explorer 10中爲'atob'和'btoa'添加了原生支持。 – some 2013-11-18 02:22:39