最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

HTML5原生支持Base64编码解码-H5教程

来源:http://erdangjiade.com/topic/133153.html H5程序员 2017-10-29 20:28浏览(214)

该方法仅支持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 < c.length);
            return d
        };
        window.atob = function(c) {
            var d = "";
            var m, k, h = "";
            var l, j, g, f = "";
            var e = 0;
            do {
                l = a.indexOf(c.charAt(e++));
                if (l < 0) {
                    continue
                }
                j = a.indexOf(c.charAt(e++));
                if (j < 0) {
                    continue
                }
                g = a.indexOf(c.charAt(e++));
                if (g < 0) {
                    continue
                }
                f = a.indexOf(c.charAt(e++));
                if (f < 0) {
                    continue
                }
                m = (l > 4);
                k = ((j & 15) > 2);
                h = ((g & 3)                     
评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2