js.common

var $I = function (a) {
    var c = document,
    b = null;
    if (typeof a == "string") {
        var j = c.getElementById(a);
        b = j;
        if (browser().ie)
            if (!b || b.attributes.id.value !== a)
                if (c.all[a])
                    for (var g = c.all[a], k = g.length, e = 0; e < k; e++)
                        if (g[e].attributes.id.value === a) {
                            b = g[e];
                            break
                        }
        if (b === null)
            if (c.getElementsByName(a))
                if (c.getElementsByName(a).length > 0)
                    b = c.getElementsByName(a)[0];
                else {
                    for (var d = [], f = c.getElementsByTagName("*"), h = f.length; h--; )
                        if (a === f[h].getAttribute("name"))
                            d[d.length] = f[h];
                    if (d.length > 0) {
                        var i = d.length - 1;
                        b = d[i]
                    }
                }
    } else if (typeof a == "object" && a.nodeType === 1)
        b = a;
    return b
}, $C = function (b, c, d) {
    var a = c || document;
    function e(f, h) {
        for (var d = [], g = new RegExp("(^|\\s)" + h + "(\\s|$)"), c = a.getElementsByTagName(f || "*"), b = null, e = c.length; e--; ) {
            b = c[e];
            g.test(b.className) && d.push(b)
        }
        return d
    }
    return document.querySelectorAll ? a.querySelectorAll("." + b) : a.getElementsByClassName ? a.getElementsByClassName(b) : e(d, b)
}, getLen = function (a) {
    return a.replace(/[^\x00-\xff]/gi, "**").length
}, isDate = function (e) {
    if (typeof e != "string")
        return false;
    var g = "-",
    c,
    a,
    b,
    f = false,
    d = e.split(g);
    if (d.length != 3)
        return false;
    c = parseInt(d[0], 10);
    a = parseInt(d[1], 10);
    b = parseInt(d[2], 10);
    if (isNaN(c) || isNaN(a) || isNaN(b) || c < 1990 || a < 1 || a > 12 || b < 1 || b > 31)
        return false;
    if ((a == 4 || a == 6 || a == 9 || a == 11) && b > 30)
        return false;
    if (a == 2) {
        if (c % 100 == 0 && c % 400 == 0 || c % 4 == 0)
            f = true;
        if (f && b > 29)
            return false;
        else if (b > 28)
            return false
    }
    return true
}, isArray = function (a) {
    return Object.prototype.toString.apply(a) === "[object Array]"
}, isOwnEmpty = function (a) {
    for (var b in a)
        if (a.hasOwnProperty(b))
            return false;
    return true
}, queryString = function (c) {
    var b = new RegExp("(^|&)" + c + "=([^&]*)(&|$)");
    try {
        var a = location.search.substr(1).match(b);
        if (!!a)
            return unescape(a[2])
    } catch (d) {
        return null
    }
    return null
}, json2Form = function (k) {
    var i = $I(k.id) || document.forms[0],
    c = k.source,
    n = k.fmt || "yyyy-MM-dd";
    if (i && c) {
        var g = [];
        if (c.rootCount) {
            if (!isNaN(parseInt(c.rootCount, 10)) && parseInt(c.rootCount, 10) > 0)
                for (var d = 0, l = parseInt(c.rootCount, 10); d < l; d++)
                    c["root" + d] != null && g.push(c["root" + d])
        } else if (c.root)
            g.push(c.root);
        else
            g.push(c);
        var m = g.length;
        function f(b) {
            var a = null;
            if (m >= 1)
                for (var e = b.substring(3), f = 0, d = {}; f < m; f++) {
                    d = g[f];
                    if (d[e]) {
                        a = d[e];
                        break
                    } else if (d[b]) {
                        a = d[b];
                        break
                    } else if (c[e]) {
                        a = c[e];
                        break
                    } else if (c[b]) {
                        a = c[b];
                        break
                    }
                }
            return a
        }
        for (var d = i.getElementsByTagName("select"), o = d.length, e = i.getElementsByTagName("input"), j = i.getElementsByTagName("textarea"), p = e.length, l = j.length, a = null, h = null, b = 0; b < p; b++) {
            h = e[b].type;
            if (h == "text" || h == "hidden") {
                a = e[b].getAttribute("id");
                if (a == null || a.length == 0)
                    a = e[b].getAttribute("name");
                if (a != null && a.length >= 3)
                    e[b].value = /^\/Date\(\d+\)\/$/.test(f(a)) ? convertJsonDate(f(a)).format(n) : f(a) || ""
            } else if (h == "checkbox" || h == "radio") {
                a = e[b].getAttribute("id");
                if (a == null || a.length == 0)
                    a = e[b].getAttribute("name");
                if (a != null && a.length >= 3)
                    e[b].checked = !!f(a)
            }
        }
        for (var a = null, b = 0; b < l; b++) {
            a = j[b].getAttribute("id");
            if (a == null || a.length == 0)
                a = j[b].getAttribute("name");
            if (a != null && a.length >= 3)
                j[b].value = f(a) || ""
        }
        for (var a = null, b = 0; b < o; b++) {
            a = d[b].getAttribute("id");
            if (a == null || a.length == 0)
                a = d[b].getAttribute("name");
            if (a != null && a.length >= 3)
                if (f(a))
                    d[b].value = f(a)
        }
    }
}, json2Select = function (i) {
    var e = i.id,
    r = i.text,
    j = i.value,
    a = i.source;
    if (e && r && j && a) {
        var k = document;
        if (e.constructor == Array)
            for (var b = null, o = null, l = null, s = null, m = null, g = e.length; g--; ) {
                s = r[g];
                m = j[g];
                o = e[g];
                b = a[o];
                if (b.root) {
                    l = b.selected;
                    b = b.root
                }
                for (var f = null, c = null, n = 0, t = b.length; n < t; n++) {
                    f = b[n];
                    c = k.createElement("option");
                    c.appendChild(k.createTextNode(f[s]));
                    c.setAttribute("value", f[m]);
                    l !== null && l === f[m] && c.setAttribute("selected", "selected");
                    $I(o).appendChild(c);
                    c = null
                }
                b = null;
                l = null
            }
        else {
            var q = null;
            if (a.root) {
                q = a.selected;
                a = a.root
            }
            for (var h = null, d = null, p = 0, u = a.length; p < u; p++) {
                h = a[p];
                d = k.createElement("option");
                d.appendChild(k.createTextNode(h[r]));
                d.setAttribute("value", h[j]);
                q !== null && q === h[j] && d.setAttribute("selected", "selected");
                $I(e).appendChild(d);
                d = null
            }
        }
    }
}, showFile = function (b) {
    var a = b.getAttribute("id") || b.getAttribute("name"),
    c = b.value || "",
    d = c.lastIndexOf("\\") + 1;
    a = a.replace("fu", "txt");
    if ($I(a)) {
        $I(a).parentNode.setAttribute("title", c);
        $I(a).value = c.substring(d)
    }
}, css = function (g, c, b) {
    var a = $I(g),
    d = c.toLowerCase();
    if (b === undefined)
        if (d == "opacity")
            return browser().ie ? a.filter && a.filter.indexOf("opacity=") >= 0 ? parseFloat(a.filter.match(/opacity=([^)]*)/)[1]) / 100 : 1 : a.style.opacity ? parseFloat(a.style.opacity) : 1;
        else {
            function f(a) {
                return a.replace(/[A-Z]/g, function (a) {
                    return "-" + a.toLowerCase()
                })
            }
            if (window.getComputedStyle)
                return window.getComputedStyle(a, null).getPropertyValue(f(c));
            if (window.document.defaultView && window.document.defaultView.getComputedStyle) {
                var e = window.document.defaultView.getComputedStyle(a, null);
                if (e)
                    return e.getPropertyValue(f(c));
                if (d == "display")
                    return "none"
            }
            return a.currentStyle ? a.currentStyle[c] : a.style[c]
        }
    else if (d == "opacity")
        if (browser().ie) {
            a.style.filter = "Alpha(Opacity=" + b * 100 + ");";
            a.style.zoom = 1
        } else
            a.style.opacity = b == 1 ? "" : "" + b;
    else {
        if (typeof b == "number")
            b += "px";
        a.style[c] = b
    }
}, attr = function (g, c, d) {
    var b = $I(g),
    f = c.toLowerCase(),
    a;
    if (d === undefined) {
        var e = null;
        switch (f) {
            case "class":
                return b.className;
            case "style":
                return b.style.cssText;
            default:
                a = b.getAttribute(c);
                e = b.getAttributeNode(c).nodeValue;
                a = a ? a : e ? e : undefined;
                return a
        }
    } else
        switch (f) {
        case "class":
            b.className += " " + d;
            break;
        case "style":
            b.style.cssText += ";" + d;
            break;
        default:
            if (isE(f))
                if (browser().ie6 || browser().ie7) {
                    a = b.getAttributeNode(c);
                    if (!a) {
                        a = document.createAttribute(c);
                        b.setAttributeNode(a)
                    }
                    a.nodeValue = d + ""
                } else
                    b.setAttribute(c, d)
    }
}, isE = function (d) {
    for (var a = true, b = ["colid", "colname", "head", "content", "headclass", "contentclass", "render", "pager", "tip", "root", "hiddcols"], c = b.length; c--; )
        if (d === b[c]) {
            a = false;
            break
        }
    return a
}, eventListeners = [], bind = function (a, b, c) {
    if (findEvent(a, b, c) !== null)
        return;
    if (!a.addEventListener)
        a.attachEvent("on" + b, c);
    else
        a.addEventListener(b, c, false);
    eventListeners.push({
        node: a,
        event: b,
        handler: c
    })
}, unbind = function (c, b, d) {
    var a = findEvent(c, b, d);
    if (a === null)
        return;
    removeEventIndex(a)
}, findEvent = function (d, c, e) {
    for (var a = null, b = eventListeners.length; b--; ) {
        a = eventListeners[b];
        if (a.node == d && a.event == c && a.handler == e)
            return b
    }
    return null
}, removeEventIndex = function (b) {
    var a = eventListeners[b];
    delete eventListeners[b];
    if (!a.node.unbind)
        a.node.detachEvent("on" + a.event, a.handler);
    else
        a.node.unbind(a.event, a.handler, false)
}, getEvent = function (a) {
    var a = a || window.event;
    if (!a) {
        var b = this.getEvent.caller;
        while (b) {
            a = b.arguments[0];
            if (a && (Event == a.constructor || MouseEvent == a.constructor))
                break;
            b = b.caller
        }
    }
    var d = a.keyCode || a.which,
    c = a.srcElement ? a.srcElement : a.target,
    e = c.nodeName,
    f = c.parentNode.nodeName;
    return {
        event: a,
        keyCode: d,
        target: c,
        current: e,
        parent: f,
        returnValue: function () {
            if (a.preventDefault)
                a.preventDefault();
            else
                a.returnValue = false
        },
        cancelBubble: function () {
            if (a.stopPropagation)
                a.stopPropagation();
            else
                a.cancelBubble = true
        }
    }
}, contextmenuEvent = function (a) {
    var b = getEvent(a);
    a = b.event;
    if (a.button == 2) {
        var c = b.target;
        document.oncontextmenu = null;
        if (c.type !== "text" && c.type !== "textarea")
            document.oncontextmenu = function (b) {
                var a = getEvent(b);
                a.cancelBubble();
                a.returnValue()
            }
    }
}, getParams = function (k, j) {
    var c = null;
    if (j != null)
        c = $I(j);
    else {
        var i = document.getElementsByTagName("table");
        if (i && i.length >= 1)
            c = i[0]
    }
    if (c) {
        for (var a = null, g = null, h = c.getElementsByTagName("select"), d = 0, l = h.length; d < l; d++) {
            a = h[d].getAttribute("id");
            if (a == null || a.length == 0)
                a = h[d].getAttribute("name");
            if (a != null && a.length > 0) {
                g = h[d].value;
                k[a] = g
            }
        }
        for (var e = c.getElementsByTagName("input"), f = null, b = 0, m = e.length; b < m; b++) {
            f = e[b].type;
            if (f == "text" || f == "hidden" || f == "checkbox" || f == "radio") {
                a = e[b].getAttribute("id");
                if (a == null || a.length == 0)
                    a = e[b].getAttribute("name");
                if (a != null && a.length > 0) {
                    g = e[b].value;
                    k[a] = g
                }
            }
        }
    }
}, ym = {
    frame: function (b) {
        var a = "__ym" + Math.random().toFixed(10) * 1e10,
        c = document.createElement("DIV");
        c.innerHTML = '<iframe style="display:none" src="about:blank" id="' + a + '" name="' + a + '" onload="ym.loaded(\'' + a + "')\"></iframe>";
        document.body.appendChild(c);
        if (b && typeof b.onComplete == "function")
            $I(a).onComplete = b.onComplete;
        return a
    },
    form: function (b, a) {
        b.setAttribute("target", a)
    },
    submit: function (b, a) {
        ym.form(b, ym.frame(a));
        return a && typeof a.onStart == "function" ? a.onStart() : true
    },
    loaded: function (c) {
        var a = $I(c),
        b;
        if (a.contentDocument)
            b = a.contentDocument;
        else if (a.contentWindow)
            b = a.contentWindow.document;
        else
            b = window.frames[c].document;
        if (b.location.href == "about:blank")
            return;
        if (typeof a.onComplete == "function")
            a.onComplete(b.body.innerHTML)
    }
}, browser = function () {
    var a = eval("''+/*@cc_on @_jscript_version@*/-0") * 1,
    b = navigator.userAgent.toLowerCase();
    return {
        version: (b.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, "0"])[1],
        ie: !!window.VBArray,
        ie6: !("1")[0],
        ie7: a === 5.7,
        ie8: !!window.XDomainRequest,
        ie9: document.documentMode && document.documentMode === 9,
        netscape: !!window.GeckoActiveXObject,
        firefox: !!window.Components,
        safari: window.openDatabase && !window.chrome,
        chrome: !!(window.chrome && window.google),
        opera: !!window.opera,
        maxthon: /maxthon/i.test(b),
        is360se: /360se/i.test(b)
    }
}, convertJsonDate = function (a) {
    return /^\/Date\(\d+\)\/$/.test(a) ? new Date(parseInt(a.replace("/Date(", "").replace(")/", ""), 10)) : new Date(a.replace(/\-/g, "/"))
}, removeNode = browser().ie ? function () {
    var a;
    return function (b) {
        if (b && b.tagName != "BODY") {
            a = a || document.createElement("DIV");
            a.appendChild(b);
            a.innerHTML = ""
        }
    }
}
() : function (a) {
    a && a.parentNode && a.tagName != "BODY" && a.parentNode.removeChild(a)
}, removeCopy = function (a) {
    var b = a.parentNode,
    c = a.nextSibling;
    removeNode(a);
    return function () {
        if (c)
            b.insertBefore(a, c);
        else
            b.appendChild(a)
    }
}, show = function (a) {
    $I(a).style.display = "block"
}, hide = function (a) {
    $I(a).style.display = "none"
}, ajax = function () {
    if (typeof arguments[0] == "object") {
        var j = arguments[0],
        a = null,
        b = {
            asyn: 1,
            type: "POST",
            data: "",
            dataType: "",
            mime: "text/html",
            charset: "GB2312",
            cache: 0,
            xhrCache: 0,
            pageCache: 1,
            timeout: 6e3,
            onComplete: function () { },
            onError: function () { },
            onLoading: function () { }

        },
        c = ajax.core.xhrUrlCache;
        for (var k in j)
            b[k] = j[k];
        if (!!!b.pageCache && c)
            for (var l in c) {
                c[l] = null;
                delete c[l]
            }
        var g = b.charset.toUpperCase(),
        m = b.timeout,
        d = b.url,
        f = null,
        e = b.type.toUpperCase(),
        h = b.dataType.toUpperCase();
        if (e == "GET")
            d += (d.match(/\?/) ? "&" : "?") + ajax.core.parseParams(b.data, g);
        f = d;
        if (e == "POST") {
            var n = d + (d.match(/\?/) ? "&" : "?") + ajax.core.parseParams(b.data, g);
            f = n
        }
        if (c[f]) {
            b.onComplete(c[f]);
            return
        }
        if (!!b.xhrCache)
            a = ajax.core.xhrCache || ajax.core.xhr();
        else
            a = ajax.core.xhr();
        if (a) {
            b.onLoading(a);
            a.open(e, d, !!b.asyn);
            a.overrideMimeType && a.overrideMimeType(b.mime);
            e == "POST" && a.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            !!!b.cache && a.setRequestHeader("If-Modified-Since", "0");
            var i = true;
            setTimeout(function () {
                i = false
            }, m);
            a.onreadystatechange = function () {
                if (i && a.readyState == 4) {
                    var j = null,
                    l = a.status;
                    if (e == "HEAD") {
                        var d = a.getAllResponseHeaders();
                        if (d != null) {
                            d = d.replace(/\r/gm, "").replace(/\n/gm, "\r\n");
                            d = d.split("\r\n");
                            for (var n = null, m = {}, p = null, o = [], q = d.length; q--; ) {
                                p = d[q];
                                o = p.split(":");
                                if (o.length >= 2) {
                                    n = o[0];
                                    m[n] = p.slice(n.length + 1)
                                }
                            }
                            c[f] = j = m;
                            b.onComplete(m)
                        }
                    }
                    if (!l && location.protocol == "file:" || l >= 200 && l <= 300 || l == 304 || browser().safari && typeof l == "undefined") {
                        if (h.length == 0) {
                            var k = a.getResponseHeader("Content-Type");
                            if (k) {
                                k = k.toUpperCase();
                                if (!!(k.indexOf("JSON") + 1))
                                    h = "JSON";
                                else if (!!(k.indexOf("XML") + 1))
                                    h = "XML"
                            }
                        }
                        switch (h) {
                            case "JSON":
                                j = function (a) {
                                    try {
                                        return JSON.parse(a)
                                    } catch (b) {
                                        try {
                                            return jsonParse(a)
                                        } catch (b) {
                                            return new Function("return " + a)()
                                        }
                                    }
                                }
                            (a.responseText || {});
                                break;
                            case "XML":
                                j = a.responseXML;
                                break;
                            case "SCRIPT":
                                ajax.core.execJs(a.responseText, g);
                                break;
                            default:
                                j = j || a.responseText
                        }
                        c[f] = j;
                        b.onComplete(j)
                    } else
                        b.onError(a)
                }
            };
            a.send(e == "POST" ? ajax.core.parseParams(b.data, g) : null)
        }
    }
};
ajax.core = {
    xhrUrlCache: {},
    xhrCache: null,
    xhr: function () {
        for (var a = [function () {
            return new ActiveXObject("Microsoft.XMLHTTP")
        }, function () {
            return new ActiveXObject("Msxml2.XMLHTTP")
        }, function () {
            return new XMLHttpRequest
        }
            ], b = a.length; b--; )
            try {
                a[b]();
                xhr = a[b];
                break
            } catch (c) { }

        return ajax.core.xhrCache = xhr()
    },
    execJs: function (e, d) {
        var b = document,
        c = b.getElementsByTagName("head")[0] || b.body,
        a = b.createElement("script");
        a.type = "text/javascript";
        a.charset = d || "gb2312";
        a.text = e;
        c.appendChild(a);
        c.removeChild(a)
    },
    parseParams: function (d, f) {
        if (typeof d !== "object")
            return d;
        else {
            var b = [],
            a = null;
            for (var c in d) {
                a = d[c];
                if (a != null && a.constructor == Array) {
                    for (var e = a.length; e--; )
                        if (a[e] != null)
                            if (f === "GB2312")
                                b.push(ajax.core.gbk(c) + "=" + ajax.core.gbk(a[e]));
                            else
                                b.push(ajax.core.utf8(c) + "=" + ajax.core.utf8(a[e]))
                } else if (a != null)
                    if (f === "GB2312")
                        b.push(ajax.core.gbk(c) + "=" + ajax.core.gbk(a));
                    else
                        b.push(ajax.core.utf8(c) + "=" + ajax.core.utf8(a));
                a = null
            }
            return b.join("&")
        }
    },
    gbk: function (a) {
        return escape(a.toString()).replace(/\+/igm, "%2B").replace(/\//igm, "%2F")
    },
    utf8: function (a) {
        return encodeURIComponent(a.toString()).replace(/\%([0-9a-zA-Z]{2})/ig, function (a) {
            return a.toLowerCase()
        })
    }
};
Date.prototype.format = function (a) {
    var b = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "w+": "日一二三四五六".charAt(this.getDay()),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds()
    };
    if (/(y+)/.test(a))
        a = a.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var c in b)
        if (new RegExp("(" + c + ")").test(a))
            a = a.replace(RegExp.$1, RegExp.$1.length == 1 ? b[c] : ("00" + b[c]).substr(("" + b[c]).length));
    return a
};
Array.prototype.unique = function () {
    for (var a = 0, c = this.length, b = []; a < c; a++)
        !b.inArray(this[a]) && b.push(this[a]);
    return b
};
Array.prototype.inArray = function (b) {
    for (var a = 0, c = this.length; a < c; a++)
        return this[a] === b ? true : false
};
String.prototype.cnLength = function () {
    var a = this.match(/[^\x00-\xff]/ig);
    return this.length + (a === null ? 0 : a.length)
};
String.prototype.trim = function () {
    var b = this.replace(/^\s+/, ""),
    a = b.length - 1,
    c = /\s/;
    while (c.test(b.charAt(a)))
        a--;
    return b.slice(0, a + 1)
};
String.prototype.strLeft = function (a) {
    var b = this.slice(0, a),
    c = b.replace(/[^\x00-\xff]/g, "**").length;
    if (c <= a)
        return b;
    c -= b.length;
    switch (c) {
        case 0:
            return b;
        case a:
            return this.slice(0, a >> 1);
        default:
            var d = a - c,
        e = this.slice(d, a),
        f = e.replace(/[\x00-\xff]/g, "").length;
            return f ? this.slice(0, d) + strLeft(e, f) : this.slice(0, d)
    }
};
document.onmousedown = contextmenuEvent;
document.oncontextmenu = function (b) {
    var a = getEvent(b);
    a.cancelBubble();
    a.returnValue()
}

你可能感兴趣的:(常用JS)