[ Index ] |
|
Code source de e107 0.7.8 |
1 if (window.ActiveXObject && !window.XMLHttpRequest) { 2 window.XMLHttpRequest = function() { 3 return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'); 4 }; 5 } 6 // Gecko support 7 /* ;-) */ 8 // Opera support 9 if (window.opera && !window.XMLHttpRequest) { 10 window.XMLHttpRequest = function() { 11 this.readyState = 0; // 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete 12 this.status = 0; // HTTP status codes 13 this.statusText = ''; 14 this._headers = []; 15 this._aborted = false; 16 this._async = true; 17 this.abort = function() { 18 this._aborted = true; 19 }; 20 this.getAllResponseHeaders = function() { 21 return this.getAllResponseHeader('*'); 22 }; 23 this.getAllResponseHeader = function(header) { 24 var ret = ''; 25 for (var i = 0; i < this._headers.length; i++) { 26 if (header == '*' || this._headers[i].h == header) { 27 ret += this._headers[i].h + ': ' + this._headers[i].v + '\n'; 28 } 29 } 30 return ret; 31 }; 32 this.setRequestHeader = function(header, value) { 33 this._headers[this._headers.length] = {h:header, v:value}; 34 }; 35 this.open = function(method, url, async, user, password) { 36 this.method = method; 37 this.url = url; 38 this._async = true; 39 this._aborted = false; 40 if (arguments.length >= 3) { 41 this._async = async; 42 } 43 if (arguments.length > 3) { 44 // user/password support requires a custom Authenticator class 45 opera.postError('XMLHttpRequest.open() - user/password not supported'); 46 } 47 this._headers = []; 48 this.readyState = 1; 49 if (this.onreadystatechange) { 50 this.onreadystatechange(); 51 } 52 }; 53 this.send = function(data) { 54 if (!navigator.javaEnabled()) { 55 alert("XMLHttpRequest.send() - Java must be installed and enabled."); 56 return; 57 } 58 if (this._async) { 59 setTimeout(this._sendasync, 0, this, data); 60 // this is not really asynchronous and won't execute until the current 61 // execution context ends 62 } else { 63 this._sendsync(data); 64 } 65 } 66 this._sendasync = function(req, data) { 67 if (!req._aborted) { 68 req._sendsync(data); 69 } 70 }; 71 this._sendsync = function(data) { 72 this.readyState = 2; 73 if (this.onreadystatechange) { 74 this.onreadystatechange(); 75 } 76 // open connection 77 var url = new java.net.URL(new java.net.URL(window.location.href), this.url); 78 var conn = url.openConnection(); 79 for (var i = 0; i < this._headers.length; i++) { 80 conn.setRequestProperty(this._headers[i].h, this._headers[i].v); 81 } 82 this._headers = []; 83 if (this.method == 'POST') { 84 // POST data 85 conn.setDoOutput(true); 86 var wr = new java.io.OutputStreamWriter(conn.getOutputStream()); 87 wr.write(data); 88 wr.flush(); 89 wr.close(); 90 } 91 // read response headers 92 // NOTE: the getHeaderField() methods always return nulls for me :( 93 var gotContentEncoding = false; 94 var gotContentLength = false; 95 var gotContentType = false; 96 var gotDate = false; 97 var gotExpiration = false; 98 var gotLastModified = false; 99 for (var i = 0; ; i++) { 100 var hdrName = conn.getHeaderFieldKey(i); 101 var hdrValue = conn.getHeaderField(i); 102 if (hdrName == null && hdrValue == null) { 103 break; 104 } 105 if (hdrName != null) { 106 this._headers[this._headers.length] = {h:hdrName, v:hdrValue}; 107 switch (hdrName.toLowerCase()) { 108 case 'content-encoding': gotContentEncoding = true; break; 109 case 'content-length' : gotContentLength = true; break; 110 case 'content-type' : gotContentType = true; break; 111 case 'date' : gotDate = true; break; 112 case 'expires' : gotExpiration = true; break; 113 case 'last-modified' : gotLastModified = true; break; 114 } 115 } 116 } 117 // try to fill in any missing header information 118 var val; 119 val = conn.getContentEncoding(); 120 if (val != null && !gotContentEncoding) this._headers[this._headers.length] = {h:'Content-encoding', v:val}; 121 val = conn.getContentLength(); 122 if (val != -1 && !gotContentLength) this._headers[this._headers.length] = {h:'Content-length', v:val}; 123 val = conn.getContentType(); 124 if (val != null && !gotContentType) this._headers[this._headers.length] = {h:'Content-type', v:val}; 125 val = conn.getDate(); 126 if (val != 0 && !gotDate) this._headers[this._headers.length] = {h:'Date', v:(new Date(val)).toUTCString()}; 127 val = conn.getExpiration(); 128 if (val != 0 && !gotExpiration) this._headers[this._headers.length] = {h:'Expires', v:(new Date(val)).toUTCString()}; 129 val = conn.getLastModified(); 130 if (val != 0 && !gotLastModified) this._headers[this._headers.length] = {h:'Last-modified', v:(new Date(val)).toUTCString()}; 131 // read response data 132 var reqdata = ''; 133 var stream = conn.getInputStream(); 134 if (stream) { 135 var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream)); 136 var line; 137 while ((line = reader.readLine()) != null) { 138 if (this.readyState == 2) { 139 this.readyState = 3; 140 if (this.onreadystatechange) { 141 this.onreadystatechange(); 142 } 143 } 144 reqdata += line + '\n'; 145 } 146 reader.close(); 147 this.status = 200; 148 this.statusText = 'OK'; 149 this.responseText = reqdata; 150 this.readyState = 4; 151 if (this.onreadystatechange) { 152 this.onreadystatechange(); 153 } 154 if (this.onload) { 155 this.onload(); 156 } 157 } else { 158 // error 159 this.status = 404; 160 this.statusText = 'Not Found'; 161 this.responseText = ''; 162 this.readyState = 4; 163 if (this.onreadystatechange) { 164 this.onreadystatechange(); 165 } 166 if (this.onerror) { 167 this.onerror(); 168 } 169 } 170 }; 171 }; 172 } 173 // ActiveXObject emulation 174 if (!window.ActiveXObject && window.XMLHttpRequest) { 175 window.ActiveXObject = function(type) { 176 switch (type.toLowerCase()) { 177 case 'microsoft.xmlhttp': 178 case 'msxml2.xmlhttp': 179 return new XMLHttpRequest(); 180 } 181 return null; 182 }; 183 } 184 185 function sendInfo() { 186 var handler = sendInfo.arguments[0]; 187 var id = sendInfo.arguments[1]; 188 var req = new XMLHttpRequest(); 189 if (req) { 190 req.onreadystatechange = function() { 191 if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { 192 document.getElementById(id).innerHTML = req.responseText; 193 } 194 }; 195 } else { 196 alert("XMLHTTPRequest failed"); 197 return null; 198 } 199 if( sendInfo.arguments.length == 2) { 200 req.open('GET', handler); 201 req.send(null); 202 } else { 203 var obj = sendInfo.arguments[2]; 204 var poststr = "ajax_used=1&"; 205 var tmp; 206 var show; 207 for(i=0; i < obj.length; i++) { 208 tmp = ""; 209 show = 1; 210 switch (obj.elements[i].type) { 211 case "text": 212 case "textarea": 213 case "hidden": 214 case "submit": 215 case "button": 216 case "password": 217 tmp = obj.elements[i].value; 218 break; 219 case "checkbox": 220 tmp = obj.elements[i].checked; 221 if(tmp) { 222 tmp = 1; 223 } else { 224 show = 0; 225 } 226 break; 227 case "select-one": 228 tmp = obj.elements[i].options[obj.elements[i].selectedIndex].value; 229 break; 230 case "radio": 231 tmp = obj.elements[i].checked; 232 if(tmp) { 233 tmp = obj.elements[i].value; 234 } else { 235 show = 0; 236 } 237 break; 238 } 239 if(show && obj.elements[i].id) { 240 tmp = tmp.replace(/&/g, "%26"); 241 poststr += obj.elements[i].id + '=' + tmp + '&'; 242 } 243 } 244 req.open('POST', handler, true); 245 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 246 req.setRequestHeader("Content-length", poststr.length); 247 req.setRequestHeader("Connection", "close"); 248 req.send(poststr); 249 } 250 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Apr 1 01:23:32 2007 | par Balluche grâce à PHPXref 0.7 |