首页 > 代码库 > 封装自己的Common.js工具库

封装自己的Common.js工具库

Code (var name in paramters) {                    //将数组中的数据以=拼接后再添加到data数组中 [name=aa,age=11]                    var _regesp = /%20/g; //空格的正则表达式                    var _value = http://www.mamicode.com/paramters[name].toString(); //获取值                    data.push(encodeURIComponent(name).replace(_regesp, ‘+‘) + "=" + encodeURIComponent(_value).replace(_regesp, ‘+‘));                }                //以&将数组元素拼接后返回 如:name=aa&age=11                return data.join("&");            },            _regCallback:function(xhr){                var _this=this;                xhr.onreadystatechange = function(){                    if (xhr.readyState == 4) {                        if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 300) {                            _this.options.success(_this._getResponseData(xhr), xhr.statusText);                        } else {                            _this.options.failure(xhr.status, xhr.statusText);                        }                    }                }            },            _getResponseData:function(xhr){                var responseType = xhr.getResponseHeader("Content-Type");                switch (responseType) {                    case ‘text/xml‘:                        return xhr.responseXML;                    case ‘text/json‘:                    case ‘text/javascript‘:                    case ‘application/javascript‘:                    case ‘application/x-javascript‘:                        return eval(‘(‘ + xhr.responseText + ‘)‘);                    default:                        return xhr.responseText;                };            },            _init:function(options){                this.options=options;                if(this.options.url=="") return;                this.xhr = this._createXhr();//创建异步对象                this._regCallback(this.xhr);//注册回调事件                //根据请求类型,发送异步请求                if(this.options.type.toLowerCase()=="post"){                    this._post(this.xhr);                }                else{                    this._get(this.xhr);                }            },            _post:function(xhr){                this.xhr=xhr;                this.data=http://www.mamicode.com/this._encodeData(this.options.data);                this.url=this.options.url+"?d=" + parseInt(Math.random()*100+1,10);                this.asyn=!!this.options.isAsyn;                xhr.open("POST", this.url, this.asyn);                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                xhr.send(this.data);            },            _get:function(xhr){                this.xhr=xhr;                this.data=http://www.mamicode.com/this._encodeData(this.options.data);                this.url=this.options.url+‘?‘ + this.data + ‘&d=‘ + parseInt(Math.random()*100+1,10);                this.asyn=!!this.options.isAsyn;                xhr.open(‘GET‘, this.url, this.asyn);                xhr.send(null);            }        });        this._init(this.options);    };    return HttpAjax;})(window);/** * 对cookie的扩展 */Object.extend(Document.cookie.prototype,{    setCookie:function(name,value,days){        var d = new Date();        d.setDate(d.getDate()+days);        document.cookie=name+‘=‘+value+‘;expires‘+d;    },    getCookie:function(name){        var arr = document.cookie.split(‘,‘);        for(var i= 0,len=arr.length; i<len; i++){            var keyV = arr[i].split(‘=‘);           if(keyV[0]==name){               return keyV[1];           }        }        return ‘‘;    },    removeCookie:function(name){        setCookie(name,‘‘,-1);    }});})(window)

有百分之六十是在网络收集而来的,因为平时用的多,但是又没有时间好好总结一下,今天终于总结到一起了,部分方法没有测试,如若有误,请指正!