HTML5 简单的js 库

(function(m) {
    var c = function(d, e) {
            if (typeof d === "object") {
                return d
            }
            if (!e) {
                e = document
            }
            return e.querySelector(d)
        },
        a = function(d, e) {
            if (!e) {
                e = document
            }
            return e.querySelectorAll(d)
        };
    c.support = function(d) {
        return {
            classList: document && document.body && !! document.body.classList,
            postMessage: !! m.postMessage
        }[d]
    };
    c.indexOf = function(d, g) {
        if (Array.prototype.indexOf) {
            return Array.prototype.indexOf.call(d, g)
        }
        var f, e;
        for (f = 0, e = d.length; f < e; f++) {
            if (d[f] === g) {
                return f
            }
        }
        return -1
    };
    c.addClass = function(e, d) {
        if (!e) {
            return
        }
        if (c.support("classList")) {
            e.classList.add(d)
        } else {
            if (c.indexOf(e.className.split(/\s+/), d) === -1) {
                e.className += " " + d
            }
        }
    };
    c.removeClass = function(j, f) {
        if (!j) {
            return
        }
        if (c.support("classList")) {
            j.classList.remove(f)
        } else {
            var d = j.className.split(/\s+/),
                g = [],
                h, e;
            for (h = 0, e = d.length; h < e; h++) {
                if (d[h] !== f) {
                    g.push(d[h])
                }
            }
            if (g.length) {
                j.className = g.join(" ")
            }
        }
    };
    c.hasClass = function(e, d) {
        if (!e) {
            return
        }
        var f;
        if (c.support("classList")) {
            f = e.classList.contains(d)
        } else {
            f = c.indexOf(e.className.split(/\s+/), d) !== -1
        }
        return f
    };
    c.urldecode = function(j) {
        var g = {},
            d = j.split("&"),
            h, f, e;
        if (!j) {
            return g
        }
        for (f = 0, e = d.length; f < e; f++) {
            h = d[f].split("=");
            g[h[0]] = decodeURIComponent(h[1]) || ""
        }
        return g
    };
    c.getQueryParams = function(d) {
        d = d ? d : location.search;
        if (d.indexOf("?") > -1) {
            d = d.split("?")[1];
            if (d.indexOf("#") > -1) {
                d = d.split("#")[0]
            }
        } else {
            if (d.indexOf("#") > -1) {
                d = d.split("#")[1]
            }
        }
        if (!d) {
            return {}
        }
        return c.urldecode(d)
    };
    c.extend = function(d) {
        d = d || {};
        var g, e, f;
        for (g = 1, e = arguments.length; g < e; g++) {
            for (f in arguments[g]) {
                if (arguments[g].hasOwnProperty(f)) {
                    d[f] = arguments[g][f]
                }
            }
        }
        return d
    };
    c.addEvent = function(f, d, e) {
        if (!f) {
            return
        }
        if (f.addEventListener) {
            f.addEventListener(d, e, false)
        } else {
            if (f.attachEvent) {
                f.attachEvent("on" + d, e)
            } else {
                f[d] = e
            }
        }
    };
    c.ajax = function(d) {
        var g, j, k, h = {
            type: "GET",
            url: "",
            data: null,
            dataType: "",
            timeout: null,
            success: null,
            error: null
        };

        function f() {
            if (j.readyState === 4) {
                if (k) {
                    clearTimeout(k)
                }
                if (j.status >= 200 && j.status < 300) {
                    if (h.success) {
                        h.success(h.dataType === "json" ? JSON.parse(j.responseText) : j.responseText, "success", j)
                    }
                } else {
                    if (h.error) {
                        h.error(j, "error", j.statusText)
                    }
                }
                j.removeEventListener("readystatechange", f);
                j = null
            }
        }
        for (g in d) {
            if (d.hasOwnProperty(g)) {
                h[g] = d[g]
            }
        }
        j = new XMLHttpRequest();
        c.addEvent(j, "readystatechange", f);
        try {
            j.open(h.type, h.url)
        } catch (i) {
            return alert("maybe ajax cross domain. " + i)
        }
        if (h.type.toUpperCase() === "POST") {
            j.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        }
        if (h.timeout) {
            k = setTimeout(function() {
                j.abort();
                j = null;
                k = null
            }, h.timeout)
        }
        j.send(h.data)
    };
    c.jsonp = function(d) {
        function f(l) {
            var j = [],
                k;
            for (k in l) {
                if (l.hasOwnProperty(k)) {
                    j.push(encodeURIComponent(k) + "=" + encodeURIComponent(l[k]))
                }
            }
            return j.join("&")
        }
        d = d || {
            url: "",
            data: {},
            jsonp: "callback",
            timeout: 20 * 1000
        };
        if (!d.url || !d.jsonp) {
            throw new Error("Need url and jsonp attributes in the options.")
        }
        var i = ("jsonp_" + (new Date().getTime()) + "_" + Math.random()).replace(".", ""),
            h = document.getElementsByTagName("head")[0],
            e = document.createElement("script");
        if (!d.data) {
            d.data = {}
        }
        d.data[d.jsonp] = i;
        h.appendChild(e);
        m[i] = function(j) {
            h.removeChild(e);
            clearTimeout(e.timer);
            m[i] = null;
            if (d.success) {
                d.success(j)
            }
        };

        function g() {
            clearTimeout(e.timer);
            m[i] = null;
            h.removeChild(e);
            if (d.fail) {
                d.fail({
                    message: "Timeout"
                })
            }
        }
        e.onerror = g;
        e.src = d.url + (d.url.indexOf("?") === -1 ? "?" : "&") + f(d.data);
        if (d.timeout) {
            e.timer = setTimeout(g, d.timeout)
        }
    };
    c.interpolate = function b(d, e) {
        if (typeof d === "string") {
            return d.replace(/{([^{}]*)}/g, function(g, f) {
                var h = e[f];
                return typeof h === "string" || typeof h === "number" ? h : g
            })
        }
        return ""
    };
    c.htmlEncode = function(d) {
        if (!d) {
            return ""
        }
        return d.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ /g, "&nbsp;").replace(/\"/g, "&quot;").replace(/\'/g, "&apos;").replace(/\n/g, "<br>")
    };
    c.ready = function(a) {
        if (document.addEventListener) {
            document.addEventListener('DOMContentLoaded', function() {
                document.removeEventListener('DOMContentLoaded', arguments.callee, false);
                a()
            }, false)
        } else if (document.attachEvent) {
            document.attachEvent('onreadystatechange', function() {
                if (document.readyState == 'complete') {
                    document.detachEvent('onreadystatechange', arguments.callee);
                    a()
                }
            })
        }
    };
    c.each = function(a, b, c) {
        var d, i = 0,
            length = a.length,
            isObj = length === undefined || typeof(a) == 'function';
        if (c) {
            if (isObj) {
                for (d in a) {
                    if (b.apply(a[d], c) === false) {
                        break
                    }
                }
            } else {
                for (; i < length;) {
                    if (b.apply(a[i++], c) === false) {
                        break
                    }
                }
            }
        } else {
            if (isObj) {
                for (d in a) {
                    if (b.call(a[d], d, a[d]) === false) {
                        break
                    }
                }
            } else {
                for (var e = a[0]; i < length && b.call(e, i, e) !== false; e = a[++i]) {}
            }
        }
        return a
    }; 
    m.$ = c; 
    m.$.prototype=c;
    m.$$ = a;
    c.fn = c.prototype;  
})(window);

 

posted @ 2015-11-03 11:56  卖果子的小男孩  阅读(282)  评论(0编辑  收藏  举报