一个XMLHttpRequest对象池
1
var XMLHttp = {
2
_objPool: [],
3
4
_getInstance: function ()
5
{
6
for (var i = 0; i < this._objPool.length; i ++)
7
{
8
if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
9
{
10
return this._objPool[i];
11
}
12
}
13
14
// IE5中不支持push方法
15
this._objPool[this._objPool.length] = this._createObj();
16
17
return this._objPool[this._objPool.length - 1];
18
},
19
20
_createObj: function ()
21
{
22
if (window.XMLHttpRequest)
23
{
24
var objXMLHttp = new XMLHttpRequest();
25
26
}
27
else
28
{
29
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
30
for(var n = 0; n < MSXML.length; n ++)
31
{
32
try
33
{
34
var objXMLHttp = new ActiveXObject(MSXML[n]);
35
break;
36
}
37
catch(e)
38
{
39
}
40
}
41
}
42
43
// mozilla某些版本没有readyState属性
44
if (objXMLHttp.readyState == null)
45
{
46
objXMLHttp.readyState = 0;
47
48
objXMLHttp.addEventListener("load", function ()
49
{
50
objXMLHttp.readyState = 4;
51
52
if (typeof objXMLHttp.onreadystatechange == "function")
53
{
54
objXMLHttp.onreadystatechange();
55
}
56
}, false);
57
}
58
59
return objXMLHttp;
60
},
61
62
// 发送请求(方法[post,get], 地址, 数据, 回调函数)
63
sendReq: function (method, url, data, callback)
64
{
65
var objXMLHttp = this._getInstance();
66
67
with(objXMLHttp)
68
{
69
try
70
{
71
// 加随机数防止缓存
72
//please cancle
this will reduce some accesses by robots ->
73
if (url.indexOf("?") > 0)
74
{
75
url += "&randnum=" + Math.random();
76
}
77
else
78
{
79
url += "?randnum=" + Math.random();
80
}
81
//end <-
82
83
open(method, url, true);
84
85
// 设定请求编码方式
86
setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
87
send(data);
88
onreadystatechange = function ()
89
{
90
if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
91
{
92
callback(objXMLHttp);
93
}
94
}
95
}
96
catch(e)
97
{
98
alert(e);
99
}
100
}
101
}
102
};
103
104
//XMLHttp.sendReq('get',url,null,showData);
105
示例:
var XMLHttp = {2
_objPool: [],3
4
_getInstance: function ()5
{6
for (var i = 0; i < this._objPool.length; i ++)7
{8
if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)9
{10
return this._objPool[i];11
}12
}13
14
// IE5中不支持push方法15
this._objPool[this._objPool.length] = this._createObj();16

17
return this._objPool[this._objPool.length - 1];18
},19

20
_createObj: function ()21
{22
if (window.XMLHttpRequest)23
{24
var objXMLHttp = new XMLHttpRequest();25

26
}27
else28
{29
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];30
for(var n = 0; n < MSXML.length; n ++)31
{32
try33
{34
var objXMLHttp = new ActiveXObject(MSXML[n]); 35
break;36
}37
catch(e)38
{39
}40
}41
} 42
43
// mozilla某些版本没有readyState属性44
if (objXMLHttp.readyState == null)45
{46
objXMLHttp.readyState = 0;47

48
objXMLHttp.addEventListener("load", function ()49
{50
objXMLHttp.readyState = 4;51
52
if (typeof objXMLHttp.onreadystatechange == "function")53
{54
objXMLHttp.onreadystatechange();55
}56
}, false);57
}58

59
return objXMLHttp;60
},61
62
// 发送请求(方法[post,get], 地址, 数据, 回调函数)63
sendReq: function (method, url, data, callback)64
{65
var objXMLHttp = this._getInstance();66

67
with(objXMLHttp)68
{69
try70
{71
// 加随机数防止缓存72
//please cancle
this will reduce some accesses by robots ->73
if (url.indexOf("?") > 0)74
{75
url += "&randnum=" + Math.random();76
}77
else78
{79
url += "?randnum=" + Math.random();80
}81
//end <-82

83
open(method, url, true);84
85
// 设定请求编码方式86
setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');87
send(data);88
onreadystatechange = function ()89
{ 90
if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))91
{92
callback(objXMLHttp);93
}94
}95
}96
catch(e)97
{98
alert(e);99
}100
}101
}102
};103

104
//XMLHttp.sendReq('get',url,null,showData);105

示例:
1
<!--
2
<td id="get_hot_pos" height="200"></td>
3
-->
4
<script type="text/javascript" src="xmlhttp.js"></script>
5
<script type="text/javascript">
6
function test(obj,objid)
7
{
document.getElementById(objid).innerHTML = obj.responseText;
8
}
9
10
11
XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_blog', '', test,'get_hot_blog');
12
//XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_fr', '', test,'get_hot_fr');
13
//XMLHttp.sendReq('GET', 'get_data.php?type=get_vp_topic', '', test,'get_vp_topic');
14
//XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_pos', '', test,'get_hot_pos');
15
</script>
<!--2
<td id="get_hot_pos" height="200"></td>3
-->4
<script type="text/javascript" src="xmlhttp.js"></script>5
<script type="text/javascript"> 6
function test(obj,objid)7
{ document.getElementById(objid).innerHTML = obj.responseText;
8
}9

10

11
XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_blog', '', test,'get_hot_blog');12
//XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_fr', '', test,'get_hot_fr');13
//XMLHttp.sendReq('GET', 'get_data.php?type=get_vp_topic', '', test,'get_vp_topic');14
//XMLHttp.sendReq('GET', 'get_data.php?type=get_hot_pos', '', test,'get_hot_pos');15
</script>

浙公网安备 33010602011771号