An Ajax connection pool Encapsulation

Source: Internet
Author: User

1. Source Code

 

This Ajax connection pool has caused me a long time. Finally, it is compatible with four types of browsers: IE6/7/8, Firefox, opera, and Google Chrome.

 

/** <Br/> * Ajax connection pool <br/> * ajaxconnectionpool build in 2009.7.10 <br/> * @ author codingmouse <br/> * @ version 1.0 <br/> * /<br/> var ajaxconnectionpool = function () {</P> <p> // task queue <br/> var taskqueue = []; <br/> // Request Buffer Pool <br/> var requestbufferpool = []; <br/> // maximum number of connections <br/> var maxthreadnumber = 2; </P> <p> return {<br/>/** <br/> * obtain idle requests <br/> * @ return {idle request object} <br/> */<br/> getidlerequest: function () {<Br/> var request = NULL; <br/> for (VAR I = 0; I <maxthreadnumber; I ++) {<br/> If (requestbufferpool [I]. readystate = 0) {<br/> request = requestbufferpool [I]; <br/> break; <br/>}< br/> return request; <br/> }, <br/>/** <br/> * initialization <br/> * @ Param {maximum number of connections} max <br/> */<br/> init: function (max) {<br/> If (max! = NULL) <br/> maxthreadnumber = max; <br/> for (var k = 0; k <maxthreadnumber; k ++) {<br/> var request = NULL; <br/> If (window. XMLHttpRequest) {// for Google Chrome, Mozilla Firefox, Netscape, opera 8.0 +, Safari, IE7 +, and other browsers <br/> request = new XMLHttpRequest (); <br/> If (request. overridemimetype) {// set the mime Category <br/>/* <br/> * some versions of browsers do not contain XML mime-type header returned by the processing server <br/> * will report an error, therefore, make sure that the returned content contains text/XML Information.. <Br/> */<br/> request. overridemimetype ("text/XML"); <br/>}< br/>} else if (window. activexobject) {// for Internet Explorer 6.0-browser <br/> var MSXML = [<br/> "msxml2.xmlhttp. 6.0 ", <br/>" msxml2.xmlhttp. 5.0 ", <br/>" msxml2.xmlhttp. 4.0 ", <br/>" msxml2.xmlhttp. 3.0 ", <br/>" msxml2.xmlhttp ", <br/>" Microsoft. XMLHTTP "]; <br/> for (VAR I = 0; I <MSXML. length; I ++) {<br/> try {<br/> request = new activexobject (MSXML [I]); <br/> break; <br/>}catch (e) {<br/> request = NULL; <br/>}< br/> If (request = NULL) {<br/> alert ("Sorry! Because you are using a browser that does not support Ajax, the server can not process a request submitted! "); <Br/> return; <br/>}< br/> requestbufferpool. push (request); <br/>}< br/> }, <br/>/** <br/> * obtain the browser type <br/> * @ return {string of the browser type} <br/> */<br/> getbrowsertype: function () {</P> <p> If (navigator. useragent. indexof ("MSIE")> 0) {<br/> return "MSIE"; // IE browser <br/>} else if (navigator. useragent. indexof ("Firefox")> 0) {<br/> return "Firefox"; // Firefox browser <br/>} else if (navigator. useragent. index Of ("safari")> 0) {<br/> return "safari"; // safan browser <br/>} else if (navigator. useragent. indexof ("Camino")> 0) {<br/> return "Camino"; // Camino browser <br/>} else if (navigator. useragent. indexof ("Gecko/")> 0) {<br/> return "gecko"; // gecko browser <br/>} else {<br/> return "unknown "; // unknown browser <br/>}</P> <p> }, <br/>/** <br/> * send a request <br/> * @ Param {Request Method post | get} method <br/> *@ param {request URL address} u RL <br/> * @ Param {data} data <br/> * @ Param {callback function} callback <br/> * @ Param {scope} scope <br/> * @ Param {whether to send an asynchronous request, note: The callback method true | false} isasync <br/> */<br/> send: function (method, URL, Data, callback, scope, isasync) {</P> <p> // XML callback handle <br/> var xmlcallbackhandler = function () {<br/> If (request. readystate <4) {<br/> window. status = "loading... "; <br/>} else if (request. readystat E = 4) {<br/> If (request. status = 200) {<br/> window. status = "finished. "; <br/> If (handler. func! = NULL) {<br/> handler. func. Call (handler. Scope! = NULL <br/>? Handler. scope <br/>: window, request. responsetext, <br/> request. responsexml, request. status); <br/>}< br/> request. abort (); <br/> If (taskqueue. length> 0) {<br/> // multiple tasks are waiting for connection in the queue. Run the first task <br/> var task = taskqueue. shift (); <br/> ajaxconnectionpool. send (task. method, task. URL, task. data, <br/> task. callback, task. scope, isasync); <br/>}< br/>} else if (request. status = 404) {<br/> window. status = "Requested URL is not found. "; <br/> alert (" requested URL is not found. "); <br/>} else if (request. status = 403) {<br/> window. status = "Access denied. "; <br/> alert (" Access denied. "); <br/>}else {<br/> window. status = "requested status is" + request. status; <br/> alert ("requested status is" + request. status); <br/>}< br/>}else {<br/> (function () {<br/> throw request. status; <br/> }). call (handl Er. Scope! = NULL? Handler. scope: Window); <br/>}< br/>}; </P> <p> // obtain idle requests <br/> var request = ajaxconnectionpool. getidlerequest (); <br/> // This is an idle request <br/> If (request! = NULL) {<br/> var handler = {<br/> FUNC: callback, <br/> scope: Scope <br/> }; <br/> If (method. tolowercase () = "get") {<br/> // Add a timestamp to prevent the browser from caching page data <br/> URL + = (URL. indexof ("? ")> 0? "&":"? ") <Br/> +" timestamp = "<br/> + new date (). gettime (); <br/>}< br/> // send a request <br/> request. open (method, URL, isasync); <br/> request. setRequestHeader (<br/> "Content-Type", <br/> (method. tolowercase () = "Post" <br/>? "Application/X-WWW-form-urlencoded;" <br/>: "text/XML") <br/>); <br/> If (ajaxconnectionpool. getbrowsertype ()! = "Firefox") {<br/> request. onreadystatechange = xmlcallbackhandler; <br/>}< br/> request. send (data); <br/> If (ajaxconnectionpool. getbrowsertype () = "Firefox") {<br/> // early call mode: request. onreadystatechange = xmlcallbackhandler (); <br/> request. onreadystatechange = xmlcallbackhandler; <br/>}< br/>}else {<br/> // when all requests are busy, the task is included in the waiting queue. <br/> var task ={< br/> method: method, <br/> URL: URL, <br/> data: data, <br/> callback: callback, <br/> scope: Scope <br/>}; <br/> taskqueue. push (task); <br/>}< br/>}; <br/> }();

 

2. Usage

 

// Obtain the idle request object <br/> var request = ajaxconnectionpool. getidlerequest (); </P> <p> // send data and use an asynchronous callback handler <br/> ajaxconnectionpool. send (method, URL, null, function () {<br/> // callback function processing logic <br/>}, window, true );

 

By codingmouse

2010.4.13

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.