Code
/* 建一个XMLHttpRequest对象池 */
/**
* XMLHttpRequest Object Pool
* @author skey <skey_chen@163.com>
*/
var __XMLHttp__ = {
_objPool: [],
_getInstance:function() {
for (var i = 0; i < this._objPool.length; i++) {
// 判断是否有未使用或者请求已经完成的对象
if (this._objPool[i].readyState == 0
|| this._objPool[i].readyState == 4
|| this._objPool[i].readyState == "complete") {
return this._objPool[i];
}
}
this._objPool[this._objPool.length] = this._createObj();// IE5中不支持push方法
return this._objPool[this._objPool.length - 1];
},
_createObj:function() {
if (window.XMLHttpRequest) {
// mozilla版本
var objXMLHttp = new XMLHttpRequest();
}
else {
// windows版本,Microsoft.XMLHTTP为IE6.0以下版本
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for(var n = 0; n < MSXML.length; n++) {
try {
var objXMLHttp = new ActiveXObject(MSXML[n]);
break;
}
catch(e) {
}
}
}
// mozilla某些版本没有readyState属性
if (objXMLHttp.readyState == null) {
objXMLHttp.readyState = 0;
objXMLHttp.addEventListener("load", function() {
objXMLHttp.readyState = 4;
if (typeof objXMLHttp.onreadystatechange == "function") {
objXMLHttp.onreadystatechange();
}
}, false);
}
return objXMLHttp;
},
// 发送请求(方法[post,get], 地址, 数据, 回调函数)
// ajax众多的frame都有callback的function, 但参数是规定好的, 在framework的prototype中都定义了
sendRequest:function(method, url, data, callback) {
var objXMLHttp = this._getInstance();
with(objXMLHttp) {
try {
// 加随机数防止缓存
if (url.indexOf("?") > 0) {
url += "&randnum=" + Math.random();
}
else {
url += "?randnum=" + Math.random();
}
open(method, url, true);
// 设定请求编码方式
setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
send(data);
onreadystatechange = function() {
if ((objXMLHttp.readyState == 4 || objXMLHttp.readyState == "complete")
&& (objXMLHttp.status == 200 || objXMLHttp.status == 304)) {
callback(objXMLHttp);// 相当于调用回调函数,并以objXMLHttp作为参数
}
}
}
catch(e) {
// alert(e);
}
}
}
}
//__XMLHttp__.sendRequest('GET', urlPage, '', returnFunction);
//function returnFunction(obj){obj.responseText;obj.responseHTML;}
/* 建一个XMLHttpRequest对象池 */
/**
* XMLHttpRequest Object Pool
* @author skey <skey_chen@163.com>
*/
var __XMLHttp__ = {
_objPool: [],
_getInstance:function() {
for (var i = 0; i < this._objPool.length; i++) {
// 判断是否有未使用或者请求已经完成的对象
if (this._objPool[i].readyState == 0
|| this._objPool[i].readyState == 4
|| this._objPool[i].readyState == "complete") {
return this._objPool[i];
}
}
this._objPool[this._objPool.length] = this._createObj();// IE5中不支持push方法
return this._objPool[this._objPool.length - 1];
},
_createObj:function() {
if (window.XMLHttpRequest) {
// mozilla版本
var objXMLHttp = new XMLHttpRequest();
}
else {
// windows版本,Microsoft.XMLHTTP为IE6.0以下版本
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for(var n = 0; n < MSXML.length; n++) {
try {
var objXMLHttp = new ActiveXObject(MSXML[n]);
break;
}
catch(e) {
}
}
}
// mozilla某些版本没有readyState属性
if (objXMLHttp.readyState == null) {
objXMLHttp.readyState = 0;
objXMLHttp.addEventListener("load", function() {
objXMLHttp.readyState = 4;
if (typeof objXMLHttp.onreadystatechange == "function") {
objXMLHttp.onreadystatechange();
}
}, false);
}
return objXMLHttp;
},
// 发送请求(方法[post,get], 地址, 数据, 回调函数)
// ajax众多的frame都有callback的function, 但参数是规定好的, 在framework的prototype中都定义了
sendRequest:function(method, url, data, callback) {
var objXMLHttp = this._getInstance();
with(objXMLHttp) {
try {
// 加随机数防止缓存
if (url.indexOf("?") > 0) {
url += "&randnum=" + Math.random();
}
else {
url += "?randnum=" + Math.random();
}
open(method, url, true);
// 设定请求编码方式
setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
send(data);
onreadystatechange = function() {
if ((objXMLHttp.readyState == 4 || objXMLHttp.readyState == "complete")
&& (objXMLHttp.status == 200 || objXMLHttp.status == 304)) {
callback(objXMLHttp);// 相当于调用回调函数,并以objXMLHttp作为参数
}
}
}
catch(e) {
// alert(e);
}
}
}
}
//__XMLHttp__.sendRequest('GET', urlPage, '', returnFunction);
//function returnFunction(obj){obj.responseText;obj.responseHTML;}