[ Index ] |
|
Code source de Seagull 0.6.1 |
1 /** 2 * XMLHttpRequest Iframe fallback 3 * 4 * http://lxr.mozilla.org/seamonkey/source/extensions/xmlextras/tests/ - should work with these 5 * 6 * @category HTML 7 * @package AJAX 8 * @author Elizabeth Smith <auroraeosrose@gmail.com> 9 * @copyright 2005 Elizabeth Smith 10 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 11 */ 12 HTML_AJAX_IframeXHR_instances = new Object(); 13 function HTML_AJAX_IframeXHR() 14 { 15 this._id = 'HAXHR_iframe_' + new Date().getTime(); 16 HTML_AJAX_IframeXHR_instances[this._id] = this; 17 } 18 HTML_AJAX_IframeXHR.prototype = { 19 // Data not sent with text/xml Content-Type will only be available via the responseText property 20 21 // properties available in safari/mozilla/IE xmlhttprequest object 22 onreadystatechange: null, // Event handler for an event that fires at every state change 23 readyState: 0, // Object status integer: 0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete 24 responseText: '', // String version of data returned from server process 25 responseXML: null, // DOM-compatible document object of data returned from server process 26 status: 0, // Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK" 27 statusText: '', // String message accompanying the status code 28 iframe: true, // flag for iframe 29 30 //these are private properties used internally to keep track of stuff 31 _id: null, // iframe id, unique to object(hopefully) 32 _url: null, // url sent by open 33 _method: null, // get or post 34 _async: null, // sync or async sent by open 35 _headers: new Object(), //request headers to send, actually sent as form vars 36 _response: new Object(), //response headers received 37 _phpclass: null, //class to send 38 _phpmethod: null, //method to send 39 _history: null, // opera has to have history munging 40 41 // Stops the current request 42 abort: function() 43 { 44 var iframe = document.getElementById(this._id); 45 if (iframe) { 46 document.body.removeChild(iframe); 47 } 48 if (this._timeout) { 49 window.clearTimeout(this._timeout); 50 } 51 this.readyState = 1; 52 if (typeof(this.onreadystatechange) == "function") { 53 this.onreadystatechange(); 54 } 55 }, 56 57 // This will send all headers in this._response and will include lastModified and contentType if not already set 58 getAllResponseHeaders: function() 59 { 60 var string = ''; 61 for (i in this._response) { 62 string += i + ' : ' + this._response[i] + "\n"; 63 } 64 return string; 65 }, 66 67 // This will use lastModified and contentType if they're not set 68 getResponseHeader: function(header) 69 { 70 return (this._response[header] ? this._response[header] : null); 71 }, 72 73 // Assigns a label/value pair to the header to be sent with a request 74 setRequestHeader: function(label, value) { 75 this._headers[label] = value; 76 return; }, 77 78 // Assigns destination URL, method, and other optional attributes of a pending request 79 open: function(method, url, async, username, password) 80 { 81 if (!document.body) { 82 throw('CANNOT_OPEN_SEND_IN_DOCUMENT_HEAD'); 83 } 84 //exceptions for not enough arguments 85 if (!method || !url) { 86 throw('NOT_ENOUGH_ARGUMENTS:METHOD_URL_REQUIRED'); 87 } 88 //get and post are only methods accepted 89 this._method = (method.toUpperCase() == 'POST' ? 'POST' : 'GET'); 90 this._decodeUrl(url); 91 this._async = async; 92 if(!this._async && document.readyState && !window.opera) { 93 throw('IE_DOES_NOT_SUPPORT_SYNC_WITH_IFRAMEXHR'); 94 } 95 //set status to loading and call onreadystatechange 96 this.readyState = 1; 97 if(typeof(this.onreadystatechange) == "function") { 98 this.onreadystatechange(); 99 } 100 }, 101 102 // Transmits the request, optionally with postable string or DOM object data 103 send: function(content) 104 { 105 //attempt opera history munging 106 if (window.opera) { 107 this._history = window.history.length; 108 } 109 //create a "form" for the contents of the iframe 110 var form = '<html><body><form method="' 111 + (this._url.indexOf('px=') < 0 ? this._method : 'post') 112 + '" action="' + this._url + '">'; 113 //tell iframe unwrapper this IS an iframe 114 form += '<input name="Iframe_XHR" value="1" />'; 115 //class and method 116 if (this._phpclass != null) { 117 form += '<input name="Iframe_XHR_class" value="' + this._phpclass + '" />'; 118 } 119 if (this._phpmethod != null) { 120 form += '<input name="Iframe_XHR_method" value="' + this._phpmethod + '" />'; 121 } 122 // fake headers 123 for (label in this._headers) { 124 form += '<textarea name="Iframe_XHR_headers[]">' + label +':'+ this._headers[label] + '</textarea>'; 125 } 126 // add id 127 form += '<textarea name="Iframe_XHR_id">' + this._id + '</textarea>'; 128 if (content != null && content.length > 0) { 129 form += '<textarea name="Iframe_XHR_data">' + content + '</textarea>'; 130 } 131 form += '<input name="Iframe_XHR_HTTP_method" value="' + this._method + '" />'; 132 form += '<s'+'cript>document.forms[0].submit();</s'+'cript></form></body></html>'; 133 form = "javascript:document.write('" + form.replace(/\'/g,"\\'") + "');void(0);"; 134 this.readyState = 2; 135 if (typeof(this.onreadystatechange) == "function") { 136 this.onreadystatechange(); 137 } 138 // try to create an iframe with createElement and append node 139 try { 140 var iframe = document.createElement('iframe'); 141 iframe.id = this._id; 142 // display: none will fail on some browsers 143 iframe.style.visibility = 'hidden'; 144 // for old browsers with crappy css 145 iframe.style.border = '0'; 146 iframe.style.width = '0'; 147 iframe.style.height = '0'; 148 149 if (document.all) { 150 // MSIE, opera 151 iframe.src = form; 152 document.body.appendChild(iframe); 153 } else { 154 document.body.appendChild(iframe); 155 iframe.src = form; 156 } 157 } catch(exception) { 158 // dom failed, write the sucker manually 159 var html = '<iframe src="' + form +'" id="' + this._id + '" style="visibility:hidden;border:0;height:0;width:0;"></iframe>'; 160 document.body.innerHTML += html; 161 } 162 if (this._async == true) { 163 //avoid race state if onload is called first 164 if (this.readyState < 3) { 165 this.readyState = 3; 166 if(typeof(this.onreadystatechange) == "function") { 167 this.onreadystatechange(); 168 } 169 } 170 } else { 171 //we force a while loop for sync, it's ugly but hopefully it works 172 while (this.readyState != 4) { 173 //just check to see if we can up readyState 174 if (this.readyState < 3) { 175 this.readyState = 3; 176 if(typeof(this.onreadystatechange) == "function") { 177 this.onreadystatechange(); 178 } 179 } 180 } 181 } 182 }, 183 184 // attached as an onload function to the iframe to trigger when we're done 185 isLoaded: function(headers, data) 186 { 187 this.readyState = 4; 188 //set responseText, Status, StatusText 189 this.status = 200; 190 this.statusText = 'OK'; 191 this.responseText = data; 192 this._response = headers; 193 if (!this._response['Last-Modified']) { 194 string += 'Last-Modified : ' + document.getElementById(this._id).lastModified + "\n"; 195 } 196 if (!this._response['Content-Type']) { 197 string += 'Content-Type : ' + document.getElementById(this._id).contentType + "\n"; 198 } 199 // if this is xml populate responseXML accordingly 200 if (this._response['Content-Type'] == 'application/xml') 201 { 202 return new DOMParser().parseFromString(this.responseText, 'application/xml'); 203 } 204 //attempt opera history munging in opera 8+ - this is a REGRESSION IN OPERA 205 if (window.opera && window.opera.version) { 206 //go back current history - old history 207 window.history.go(this._history - window.history.length); 208 } 209 if (typeof(this.onreadystatechange) == "function") { 210 this.onreadystatechange(); 211 } 212 document.body.removeChild(document.getElementById(this._id)); 213 }, 214 215 // strip off the c and m from the url send...yuck 216 _decodeUrl: function(querystring) 217 { 218 //opera 7 is too stupid to do a relative url...go figure 219 var url = unescape(location.href); 220 url = url.substring(0, url.lastIndexOf("/") + 1); 221 var item = querystring.split('?'); 222 //rip off any path info and append to path above <- relative paths (../) WILL screw this 223 this._url = url + item[0].substring(item[0].lastIndexOf("/") + 1,item[0].length); 224 if(item[1]) { 225 item = item[1].split('&'); 226 for (i in item) { 227 var v = item[i].split('='); 228 if (v[0] == 'c') { 229 this._phpclass = v[1]; 230 } else if (v[0] == 'm') { 231 this._phpmethod = v[1]; 232 } 233 } 234 } 235 if (!this._phpclass || !this._phpmethod) { 236 var cloc = window.location.href; 237 this._url = cloc + (cloc.indexOf('?') >= 0 ? '&' : '?') + 'px=' + escape(HTML_AJAX_Util.absoluteURL(querystring)); 238 } 239 } 240 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 01:27:52 2007 | par Balluche grâce à PHPXref 0.7 |