国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

自己動手封裝的 ajax

2024-09-01 08:29:25
字體:
來源:轉載
供稿:網友
以前開發用了很多AJAX的技術比如EXT,prototype,jQuery等等,但都是開源封裝好的AJAX框架。從沒真正用過純正的AJAX,故參照prototyp面向對象思想自己封裝了一個AJAX框架。希望能給讀者參考、幫助、評價。
代碼如下:
/*
* 自己封裝的ajax
*
*
* @author 姜松
* @version 1.00 $date:2009-07-02
*
* history:
*
*/
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
Object.extend(String.prototype, {
include: function(pattern) {
return this.indexOf(pattern) > -1;
},
startsWith: function(pattern) {
return this.indexOf(pattern) == 0;
},
endsWith: function(pattern) {
return this.lastIndexOf(pattern) == (this.length - pattern.length);
},
empty: function() {
return /^/s*$/.test(this) || this == undefined || this == null;
}
});
Object.extend(Array.prototype, {
each: function(iterator) {
try {
for (var i = 0, length = this.length; i < length; i++) {
iterator(this[i]);
}
} catch (e) {
if (e != 'break') { throw e };
}
},
clear: function() {
this.length = 0;
return this;
},
first: function() {
return this[0];
},
last: function() {
return this[this.length - 1];
},
indexOf: function(object) {
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] == object) {return i};
}
return -1;
},
size: function() {
return this.length;
},
include: function(object) {
var found = false;
this.each(function(value) {
if (value == object) {found = true; throw 'break';}
});
return found;
}
});
function $(element) {
if(arguments.length > 1) {
for(var i = 0, elements = [], length = arguments.length; i < length; i++) {
elements.push($(arguments[i]));
}
return elements;
}
if(typeof element == 'string') {
element = document.getElementById(element);
}
return element;
};
var ajax = {
transport: new Object(),
options: new Object(),
getTransport: function() {
if(window.ActiveXObject) {
try {
return new ActiveXObject('Msxm12.XMLHTTP');
} catch(e) {
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {}
}
} else if(window.XMLHttpRequest) {
try {
return new XMLHttpRequest();
} catch(e) {}
}
},
setOptions: function(options) {
ajax.options = {
method: 'get',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'utf-8',
parameters: ''
};
Object.extend(ajax.options, options);
ajax.options.method = ajax.options.method.toUpperCase();
},
request: function(url, options) {
ajax.transport = ajax.getTransport();
ajax.setOptions(options);
this.method = ajax.options.method;
var params = ajax.options.parameters;
if (!['GET', 'POST'].include(this.method)) {
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奉新县| 饶平县| 白玉县| 黎平县| 新营市| 黄梅县| 江门市| 克拉玛依市| 武平县| 织金县| 大关县| 阿勒泰市| 朝阳区| 南溪县| 徐水县| 松溪县| 安庆市| 金湖县| 黄骅市| 尼玛县| 壤塘县| 牡丹江市| 龙井市| 铁岭县| 石河子市| 荥经县| 阿拉善盟| 巴青县| 喜德县| 阳山县| 通榆县| 教育| 翁牛特旗| 台南市| 延边| 沁水县| 黄平县| 乌恰县| 永春县| 尖扎县| 安泽县|