1// Utf-8 é 2 3function ajaxObject (maxSumiltaniousConnectionParam) 4{ 5 // each XMLHttpRequest will be stored in that array 6 this.ajaxRequestArray = new Array(); 7 8 // should the requests be async 9 // set this value with setAsync(true or false) 10 this.async = true; 11 12 // set the maximum simultatius connection, 10 is the default one if none was provided 13 this.maxSumiltaniousConnection = maxSumiltaniousConnectionParam; 14 if(maxSumiltaniousConnectionParam == undefined) 15 { 16 this.maxSumiltaniousConnection = 10; // hardcoded maximum connection 17 } 18 19 this.replace = function (url, id, method, arg, loadingFunc, loadedFunc) 20 { 21 var validate = document.getElementById(id); 22 if(validate != null) 23 { 24 if(typeof(validate.innerHTML) != undefined) 25 { 26 this.request(url, '', method, arg, loadingFunc, loadedFunc, id); 27 } 28 } 29 } 30 31 this.request = function (url, callback, method, arg, loadingFunc, loadedFunc, replaceId) 32 { 33 // validation of the inputs 34 method = method.toLowerCase(); 35 if(url == undefined) { return false; } 36 else if(typeof(callback) != "function" && replaceId == "") { return false; } 37 else if(method != "post" && method != "get") { return false; } 38 39 // assign inner variable with the provided value 40 var innerCallback = callback; 41 var innerLoadingFunc = loadingFunc; 42 var innerLoadedFunc = loadedFunc; 43 var innerReplaceId = replaceId; 44 // get an empty or new XMLHttpRequest object 45 var httpRequest = this.getHttpRequest(); 46 47 // continue if connection is clear 48 if(httpRequest && (httpRequest.readyState == 4 || httpRequest.readyState == 0)) 49 { 50 // an exception can be thrown by the open and send method 51 try { 52 // call the loading function 53 if(typeof(innerLoadingFunc) == "function") { 54 innerLoadingFunc(); 55 } 56 else if(typeof(outterLoadingFunc) == "function") { 57 outterLoadingFunc(innerLoadingFunc); 58 } 59 // open and send the request 60 if(method == "get") { 61 arg+="&IECacheProblem="+IECacheFix_getTime(); 62 var urlComplete = url; 63 if(arg != undefined) { 64 urlComplete += '?'+arg; 65 } 66 httpRequest.open("get", urlComplete, this.async); 67 //use the internal handle function (needs to be located here because IE sucks and need it to be between open and send) 68 var readystatechange = getHandleStateChange(this) 69 httpRequest.onreadystatechange = readystatechange; 70 httpRequest.send(null); 71 // the readystatechange is never called if in sync mode, so call it when returned from the server 72 if(!this.async) 73 readystatechange(); 74 } 75 else { 76 httpRequest.open("post", url, this.async); 77 httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset="+this.encoding); 78 //use the internal handle function (needs to be located here because IE sucks and need it to be between open and send) 79 var readystatechange = getHandleStateChange(this) 80 httpRequest.onreadystatechange = readystatechange; 81 if(arg=="" || arg == undefined) { 82 arg = "IECacheProblem="+IECacheFix_getTime(); 83 } 84 httpRequest.send(arg); 85 // the readystatechange is never called if in sync mode, so call it when returned from the server 86 if(!this.async) 87 readystatechange(); 88 } 89 } 90 catch(e) { 91 92 } 93 } 94 // if the connection is not clear, try again in a little while 95 else { 96 // create the callback function 97 function getDelayedRequest(thisAjax) { 98 function delayedRequest() { 99 thisAjax.request(url, (typeof(callback) == "function")?callback:'', method, arg, loadingFunc, loadedFunc, replaceId); 100 } 101 return delayedRequest; 102 } 103 setTimeout(getDelayedRequest(this), 500); 104 } 105 106 107 // generic handle function 108 function getHandleStateChange(thisAjax) { 109 function handleStateChange () { 110 if(httpRequest.readyState == 4) { 111 // status == 200 when the connection went well 112 if(httpRequest.status == 200) { 113 if(typeof(innerCallback) == "function") { 114 // call the callback function with the return text and XML (if any) 115 xmlResponse = httpRequest.responseXML; 116 if(!xmlResponse || !xmlResponse.documentElement) { 117 118 xmlResponse = false;
119 } 120 if(xmlResponse.documentElement && xmlResponse.documentElement.nodeName == 'parseerror') { 121 122 xmlResponse = false; 123 } 124 125 // call the loaded function, the reason why this is in the if AND in the else is that if this function 126 // takes to much time to return and another ajax call is made with the same object a exception can occur 127 if(typeof(innerLoadedFunc) == "function") { innerLoadedFunc(); } 128 else if(typeof(outterLoadedFunc) == "function") { outterLoadedFunc(innerLoadedFunc); } 129 130 131 // first param is the true, complete response from (body, no headers) the server returned, 132 // second param is the xml returned or false if invalid xml 133 innerCallback(httpRequest.responseText, xmlResponse); 134 } 135 else { 136 var text = httpRequest.responseText; 137 138 // call the loaded function, the reason why this is in the if AND in the else is that if this function 139 // takes to much time to return and another ajax call is made with the same object a exception can occur 140 if(typeof(innerLoadedFunc) == "function") { innerLoadedFunc(httpRequest.responseText); } 141 else if(typeof(outterLoadedFunc) == "function") { outterLoadedFunc(innerLoadedFunc(httpRequest.responseText)); } 142 143 // this code is only executed with to replace an object.innerHTML with the responseText 144 var r = document.getElementById(innerReplaceId); 145 if(r) { 146 r.innerHTML = text; 147 } 148 } 149 } 150 else { 151 // status is not 200, display the status to the user (404: not found, 403: acces denied, 500: internal error) 152 // TODO: setup a callback function to handle this event 153 if (httpRequest.status == 0) { 154 // TODO: setup a call back function to handle this event, ignore the error for now 155 } else { 156 157 } 158 } 159 } 160 } 161 return handleStateChange; 162 } 163 164 165 return true; 166 } 167 168 // get a free or newly created XMLHttpRequest object 169 this.getHttpRequest = function () { 170 var httpRequest = undefined; 171 // loop trought the already created object to find one free 172 for(i=0; i<this.ajaxRequestArray.length; i++) { 173 // if one was found assign it and break the loop 174 if(this.ajaxRequestArray[i].readyState == 4 || this.ajaxRequestArray[i].readyState == 0) { 175 httpRequest = this.ajaxRequestArray[i]; 176 break; 177 } 178 } 179 // if no free object was found and that the max simultatious connection is not reach create a new one 180 if(httpRequest == undefined && this.ajaxRequestArray.length < this.maxSumiltaniousConnection) { 181 // try to create a new object for the mordern browser 182 try { 183 httpRequest = new XMLHttpRequest(); 184 } catch(e) { 185 // try to create object for older browser 186 var xmlHttpVersion = new Array("Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"); 187 for(var i = 0; i < xmlHttpVersion.length && !httpRequest; i++) { 188 try { 189 httpRequest = new ActiveXObject(xmlHttpVersion[i]); 190 } catch(e) { } 191 } 192 } 193 // get the next index for keeping the object 194 var nextIndex = this.ajaxRequestArray.length; 195 // assign the new object to the list 196 this.ajaxRequestArray[nextIndex] = httpRequest; 197 } 198 // return the free object, the new object or false if the max is reach and all are busy 199 return httpRequest; 200 } 201 202 // these variables are used to store the callbacks function that will fire on starting a call, and end when the response is received. 203 var outterLoadingFunc = ''; 204 var outterLoadedFunc = '';
205 // call setLoadingFunction and setLoadedFunction to set the function to be called when a starting a call to the server (request or replace) and when the response is received. 206 // the callback function should take 1 parameter each, this parameter will be what you pass in the request or replace call. 207 this.setLoadingFunction = function (callback) { 208 outterLoadingFunc = callback; 209 } 210 this.setLoadedFunction = function (callback) { 211 outterLoadedFunc = callback; 212 } 213 214 // set the function that will be called if an error occurs 215 var errorCallback = ''; 216 var lastError = new Array(0,''); 217 this.setErrorCallback = function (callback) { 218 errorCallback = callback; 219 } 220 221 222 // text-encoding for the transmision with the server, user function setEncoding to set it 223 this.encoding = 'utf-8'; 224 // text-encoding for the transmision with the server, user function setEncoding to set it 225 this.setEncoding = function(newCharset) { 226 var old = this.encoding; 227 this.encoding = newCharset; 228 return old; 229 } 230 231 this.setAsync = function(newValue) { 232 this.async = newValue; 233 } 234 235 // transform the form elements into url encoded arguments (name=value&name2=value2) 236 this.formToUrl = function(formId) { return this.formToUrlArguments(formId); } 237 this.formToUrlArgument = function(formId) { return this.formToUrlArguments(formId); } 238 this.formToUrlArguments = function(formId) { 239 var form = false; 240 // you can pass ether the object or the id of the form 241 if(typeof(formId) == "object") { 242 form = formId; 243 } 244 else { 245 form=document.getElementById(formId); 246 } 247 248 // if form is an object 249 if(form != null && typeof(form) == "object" && form.tagName.toLowerCase() == "form") { 250 // r is the return string... yes, I know, this name could be more descriptive. 251 var r = ""; 252 // loop trought all of the form elements 253 for(var i = 0; i < form.elements.length; i++) { 254 var formElement = form.elements[i]; 255 // will not include disabled or unamed element (like a submit button <input type="submit" value="Send"/>) 256 if(formElement.name != "" && formElement.disabled == false) { 257 // text, hidden, password, textarea and single select (dropdown) are all passed the same way, basic text 258 if(formElement.type == "text" || formElement.type == "hidden" || formElement.type == "password" || formElement.type == "textarea" || formElement.type == "select-one") { 259 r+= encodeURIComponent(formElement.name)+"="+encodeURIComponent(formElement.value)+"&"; 260 } 261 // checkbox are send but the value is only sent if the checkbox is checked 262 else if (formElement.type == "checkbox") { 263 r+= encodeURIComponent(formElement.name)+"="; 264 if (formElement.checked == true) { 265 r+= encodeURIComponent(formElement.value)+"&"; 266 } 267 r+= "&" 268 } 269 // only the selected radio button and value is send 270 else if (formElement.type == "radio" && formElement.checked) { 271 r+= encodeURIComponent(formElement.name)+"="; 272 r+= encodeURIComponent(formElement.value)+"&"; 273 } 274 // for a case like the multiple select (listbox) an array of value is send 275 else if(formElement.type == "select-multiple") { 276 for(var i = 0;i < formElement.length;i++){ 277 if(formElement.options[i].selected == true){ 278 //TODO: this will work with PHP but as not been tested with ASP 279 r+= encodeURIComponent(formElement.name)+"[]="+encodeURIComponent(formElement.options[i].value)+"&"; 280 } 281 } 282 } 283 284 else if(formElement.type == "file") { 285 286 } 287 } 288 } 289 return r; 290 } 291 else { 292 } 293 return false;
294 } 295 296 return this; 297} 298 299function IECacheFix_getTime() { 300 var d = new Date(); 301 return d.getTime(); 302} 303/**/
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.