项目需要临时做的,尚未优化,但已足够应付大部分应用了,兼容多浏览器,无BUG。此类中加入了队列方式,在实例化对象时设置第一个参数为大于0的数就会开启,多请求时如果超过这个数值就会暂存到队列中稍后执行。
13 | function $$(id){ return document.getElementById(id);} |
14 | function xdh_ajax(_poolSize,_instanceName){ |
15 | this .ajaxPool = new Array(); |
16 | this .queryArr = new Array(); |
17 | this .instanceName = '' ; |
22 | this .createXMLhttp = function (){ |
23 | if ( typeof (ajax.ajaxPool) == null ) ajax.ajaxPool = new Array(); |
24 | if (window.XMLHttpRequest){ |
25 | this .ajaxPool[_nextNum] = new XMLHttpRequest(); |
26 | } else if (window.ActiveXObject){ |
27 | this .ajaxPool[_nextNum] = new ActiveXObject( "Microsoft.XMLHTTP" ); |
29 | _nextNum = _nextNum + 1; |
33 | this .init = function (_poolSize,_instanceName){ |
34 | if (!isNaN(_poolSize) && _poolSize != 0) this .poolSize = _poolSize; |
35 | if ( typeof (_instanceName) == 'undefined' ){ |
38 | } else this .instanceName = _instanceName; |
42 | this .getInstance = function (){ |
43 | for (_i in this .ajaxPool){ |
44 | if ( this .ajaxPool[_i].readyState == 0 || this .ajaxPool[_i].readyState == 4){ |
45 | this .ajaxPool[_i].onreadystatechange = function (){}; |
46 | return this .ajaxPool[_i]; |
49 | if ( this .poolSize == 0 || _nextNum < this .poolSize){ |
51 | return this .ajaxPool[_nextNum - 1]; |
59 | this .queryRightAway = function (_method,_url,_func,_data){ |
60 | if (window.XMLHttpRequest){ |
61 | var _o = new XMLHttpRequest(); |
62 | } else if (window.ActiveXObject){ |
63 | var _o = new ActiveXObject( "Microsoft.XMLHTTP" ); |
66 | var joinstr = 'ts=' +Math.random(); |
67 | joinstr = (_url.indexOf( '?' ) > -1) ? '&' +joinstr : '?' +joinstr; |
68 | _o.open( 'GET' ,_url+joinstr, true ); |
70 | } else if (_method == 'POST' ){ |
71 | var _sendData = 'value=' +encodeURIComponent(_data); |
72 | _o.open( 'POST' ,_url, true ); |
73 | _o.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); |
76 | if ( typeof (_func) != 'undefined' && _func != '' ){ |
77 | if ( typeof (_func) == 'object' ){ |
79 | for ( var _i=1;_i<_func.length;_i++){ |
80 | _param = _param + ',"' + _func[_i] + '"' ; |
82 | _o.onreadystatechange = function (){ if (_o.readyState == 4){eval( '_func[0](_o' +_param+ ');' );}}; |
84 | _o.onreadystatechange = function (){ if (_o.readyState == 4){_func(_o);}}; |
90 | this .addQueryToArr = function (_method,_url,_func,_data){ |
91 | this .queryArr.push(Array(_method,_url,_func,_data)); |
95 | this .getQueryFromArr = function (){ |
96 | return this .queryArr.shift(); |
100 | this .checkQuery = function (_method,_url,_func,_data){ |
101 | var _obj = this .getInstance(); |
102 | if (_obj === false ) return false ; |
104 | if ( typeof (_method) != 'undefined' ) this .addQueryToArr(_method,_url,_func,_data); |
107 | if ( this .queryArr.length != 0){ |
108 | if ( typeof (_method) != 'undefined' ) this .addQueryToArr(_method,_url,_func,_data); |
109 | var temp = this .getQueryFromArr(); |
111 | var temp = Array(_method,_url,_func,_data); |
113 | temp = Array(_obj,temp[0],temp[1],temp[2],temp[3]); |
123 | this .query = function (_method,_url,_func,_data,_rightaway){ |
125 | this .queryRightAway(_method,_url,_func,_data); |
128 | var _q = this .checkQuery(_method,_url,_func,_data); |
129 | if (_q === false ) return false ; |
130 | if (_q === true ) return true ; |
132 | var joinstr = 'ts=' +Math.random(); |
133 | joinstr = (_q[2].indexOf( '?' ) > -1) ? '&' +joinstr : '?' +joinstr; |
134 | _q[0].open( 'GET' ,_q[2]+joinstr, true ); |
136 | } else if (_q[1] == 'POST' ){ |
137 | var _sendData = 'value=' +encodeURIComponent(_q[4]); |
138 | _q[0].open( 'POST' ,_q[2], true ); |
139 | _q[0].setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); |
140 | _q[0].send(_sendData); |
142 | var callback = ( this .poolSize != 0) ? 'if(' + this .instanceName+ '.queryArr.length != 0)' + this .instanceName+ '.query();' : '' ; |
145 | callback += 'if(' + this .instanceName+ '){' + this .instanceName + '.release();}' ; |
146 | if ( typeof (_q[3]) != 'undefined' && _q[3] != '' ){ |
147 | if ( typeof (_q[3]) == 'object' ){ |
149 | for ( var _i=1;_i<_q[3].length;_i++){ |
150 | _param = _param + ',"' + _q[3][_i] + '"' ; |
152 | _q[0].onreadystatechange = function (){ if (_q[0].readyState == 4){eval( '_q[3][0](_q[0]' +_param+ ');' +callback);}}; |
154 | _q[0].onreadystatechange = function (){ if (_q[0].readyState == 4){eval( '_q[3](_q[0]);' +callback);}}; |
160 | this .release = function (isabort){ |
161 | for (_i in this .ajaxPool){ |
163 | this .ajaxPool[_i].onreadystatechange = function (){}; |
164 | this .ajaxPool[_i].abort(); |
166 | if ( this .ajaxPool[_i].readyState == 0 || this .ajaxPool[_i].readyState == 4){ |
167 | this .ajaxPool.splice(_i,1); |
168 | _nextNum = _nextNum - 1; |
174 | this .init(_poolSize,_instanceName); |
178 | function closewindow(){ |
179 | if ( typeof (ajax) != 'undefined' && ajax != null ){ |
181 | _unfinishedTotal = _unfinished = 0; |
182 | while (ajax.ajaxPool.length != 0){ |
188 | window.onbeforeunload = window.onunload = closewindow; |
在你的回调函数中必须声明至少一个参数,此参数为一个XMLHttpRequest对象实例,具体请看下面示例。
release方法请在适当的时候自行调用,以免意外发生。
一个简单的应用例子,获取服务器时间。
xdh_reponse.php
2 | die ( date ( 'Y-m-d H:i:s' )); |
xdh_request.html
3 | < meta http-equiv = "Content-Type" content = "text/html; charset=gb2312" /> |
4 | < script language = "javascript" type = "text/javascript" src = "xdh_ajax.js" ></ script > |
5 | < script language = "javascript" type = "text/javascript" > |
6 | var ajax = new xdh_ajax(0,'ajax'); |
8 | $$('serverTime').innerHTML = _obj.responseText; |
11 | ajax.query('GET','xdh_reponse.php',update); |
18 | < div id = "serverTime" ></ div > |
19 | < input type = "button" onclick = "query()" value = "更新" /> |