28个常用JavaScript方法集锦(5)


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) {
            return decodeURIComponent(c.substring(nameEQ.length, c.length))
        }
    } return null
}

  

/*  Ajax 请求  */

复制代码 代码如下:


C.ajax = function (args) {
    var self = this;
    this.options = {
        type: 'GET',
        async: true,
        contentType: 'application/x-www-form-urlencoded',
        url: 'about:blank',
        data: null,
        success: {},
        error: {}
    };
    this.getXmlHttp = function () {
        var xmlHttp;
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if (!xmlhttp) {
            alert('您的浏览器不支持AJAX');
            return false;
        }
        return xmlhttp;
    };
    this.send = function () {
        C.each(self.options, function (key, val) {
            self.options[key] = (args[key] == null) ? val : args[key];
        });
        var xmlHttp = new self.getXmlHttp();
        if (self.options.type.toUpperCase() == 'GET') {
            xmlHttp.open(self.options.type, self.options.url + (self.options.data == null ? "" : ((/[?]$/.test(self.options.url) ? '&' : '?') + self.options.data)), self.options.async);
        }
        else {
            xmlHttp.open(self.options.type, self.options.url, self.options.async);
            xmlHttp.setRequestHeader('Content-Length', self.options.data.length);
        }
        xmlHttp.setRequestHeader('Content-Type', self.options.contentType);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200 || xmlHttp.status == 0) {
                    if (typeof self.options.success == 'function') self.options.success(xmlHttp.responseText);
                    xmlHttp = null;
                }
                else {
                    if (typeof self.options.error == 'function') self.options.error('Server Status: ' + xmlHttp.status);
                }
            }
        };
        xmlHttp.send(self.options.type.toUpperCase() == 'POST' ? self.options.data.toString() : null);
    };
    this.send();
};

/*  JS StringBuilder 用法  */

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxwsf.html