日期:2014-05-16 浏览次数:20526 次
//Ajax扩展
var Ajax = function(url, params, callback) {
var reqError = "\u54cd\u5e94\u5f02\u5e38\uff01\uff01\uff01";
var sendData = null;
var createXHR = function () {
var XHR;
if (window.XMLHttpRequest) {
XHR = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
try {
XHR = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XHR = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
}
return XHR;
};
var processParams = function () {
var ret = "";
for (var p in params) {
ret += "&";
ret += p + "=" + params[p];
}
ret = ret.substring(1);
return ret;
};
var method = (url.indexOf("?") == -1) ? "POST" : "GET";
if (params&&typeof(params)=='object') {
if(typeof(params)=='object'){
if (method == "GET") {
url += "&" + processParams();
} else {
sendData = processParams();
}
}
if(typeof(params)=='string'){
if (method == "GET") {
url += "&" + params;
} else {
sendData = params;
}
}
}
var xhr = createXHR();
xhr.open(method, url, true);
if (method == "POST") {
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if(callback){
callback(xhr);
};
} else {
window.alert(reqError);
}
}
};
xhr.send(sendData);
};
//Array扩展
Array.prototype.indexOf=function (el) {
var ret=-1;
for(var k=0;k<this.length;k++) {
if(this[k]==el) {
ret=k;
break;
}
}
return ret;
};
Array.prototype.each=function (fn) {
for(var k=0;k<this.length;k++) {
fn(this[k],k);
}
};
Array.prototype.unique=function () {
var temp={};
var result=[];
for(var k=0;k<this.length;k++) {
if(!temp[this[k]]) {
temp[this[k]]=true;
}
}
for(var i in temp) {
result.push(i);
}
return result;
};
Array.prototype.remove=function (el) {
while(this.indexOf(el)!=-1) {
this.splice(this.indexOf(el),1);
}
return this;
};
var Map=function () {
this.map={};
this.keys=[];
this.values=[];
this.count=0;
this.add=function (k,v) {
this.map[k]=v;
var index=this.keys.indexOf(k);
if(index!=-1) {
this.values[index]=v;
}else {
this.keys.push(k);
this.values.push(v);
this.count++;
}
};
this.get=function (k) {
return this.map[k];
}
};
//Function扩展
Function.prototype.defer=function () {
var time=arguments[0]?arguments[0]:1000;
var params=[];
for(var k=1;k<arguments.length;k++) {
params.push(arguments[k]);
}
params=params.join(",");
window.setTimeout(this(params),time);
};
?