HTML5原生支持Base64编码解码
程序员文章站
2022-04-17 15:30:37
...
该方法仅支持IE10+、chrome、等现代浏览器。
适合用于原生支持的
(function(){ var Base64 = { encode : function(str){ return window.btoa(unescape(encodeURIComponent(str))); }, decode : function(str){ return decodeURIComponent(escape(window.atob(str))); } }; window.BASE64 = Base64; })();
旧版本兼容
(function() { if (!window.btoa) { var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; window.btoa = function(c) { var d = ""; var m, k, h = ""; var l, j, g, f = ""; var e = 0; do { m = c.charCodeAt(e++); k = c.charCodeAt(e++); h = c.charCodeAt(e++); l = m >> 2; j = ((m & 3) > 4); g = ((k & 15) > 6); f = h & 63; if (isNaN(k)) { g = f = 64 } else { if (isNaN(h)) { f = 64 } } d = d + a.charAt(l) + a.charAt(j) + a.charAt(g) + a.charAt(f); m = k = h = ""; l = j = g = f = "" } while (e > 4); k = ((j & 15) > 2); h = ((g & 3)