[ Index ] |
|
Code source de Horde 3.1.3 |
1 /** 2 * Javascript library for working with XmlHttpRequest objects and 3 * Horde. 4 * 5 * $Horde: horde/js/httpclient.js,v 1.2.10.3 2005/10/18 11:33:38 jan Exp $ 6 * 7 * See the enclosed file COPYING for license information (LGPL). If you did not 8 * receive this file, see http://www.fsf.org/copyleft/lgpl.html. 9 */ 10 11 // Constructor for generic HTTP client. 12 function HTTPClient() {}; 13 14 // Add methods and properties as array. 15 HTTPClient.prototype = { 16 url: null, 17 18 // Instance of XMLHttpRequest. 19 request: null, 20 21 // Used to make sure multiple calls are not placed with the same 22 // client object while another in progress. 23 callInProgress: false, 24 25 // The user defined handler - see MyHandler below. 26 userhandler: null, 27 28 init: function(url) 29 { 30 this.url = url; 31 32 try { 33 // Mozilla, Safari. 34 this.request = new XMLHttpRequest(); 35 } catch (e) { 36 // IE. 37 var MSXML_XMLHTTP_PROGIDS = new Array( 38 "MSXML2.XMLHTTP.4.0", 39 "MSXML2.XMLHTTP.3.0", 40 "MSXML2.XMLHTTP", 41 "Microsoft.XMLHTTP" 42 ); 43 var success = false; 44 for (var i = 0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) { 45 try { 46 this.request = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]); 47 success = true; 48 } catch (e) {} 49 } 50 if (!success) { 51 throw "Unable to create XMLHttpRequest."; 52 } 53 } 54 }, 55 56 // Handler argument is a user defined object to be called. 57 asyncGET: function(handler) 58 { 59 // Degrade or some such. 60 if (!this.request) { 61 return false; 62 }; 63 64 // Prevent multiple calls 65 if (this.callInProgress) { 66 throw "Call in progress"; 67 }; 68 69 this.callInProgress = true; 70 71 this.userhandler = handler; 72 73 // Open an async request - third argument makes it 74 // asynchronous. 75 this.request.open('GET', this.url, true); 76 77 // Have to assign "this" to a variable. 78 var self = this; 79 80 // Assign a closure to the onreadystatechange callback. 81 this.request.onreadystatechange = function() 82 { 83 self.stateChangeCallback(self); 84 } 85 86 this.request.send(null); 87 }, 88 89 stateChangeCallback: function(client) 90 { 91 switch (client.request.readyState) { 92 // Request not yet made. 93 case 1: 94 try { 95 client.userhandler.onInit(); 96 } catch (e) { /* Handler method not defined. */ } 97 break; 98 99 // Contact established with server but nothing downloaded 100 // yet. 101 case 2: 102 try { 103 status = client.request.status; 104 // Check for HTTP status 200. 105 if (status != 200) { 106 client.userhandler.onError( 107 status, 108 client.request.statusText 109 ); 110 111 // Abort the request. 112 client.request.abort(); 113 114 // Call no longer in progress. 115 client.callInProgress = false; 116 } 117 } catch (e) { 118 /* MSXMLHTTP 3.x+ doesn't populate status until 119 * readyState 4. */ 120 } 121 break; 122 123 // Called multiple times while download is in progress. 124 case 3: 125 // Notify user handler of download progress. 126 try { 127 // Get the total content length (useful to work 128 // out how much has been downloaded). 129 try { 130 var contentLength = 131 client.request.getResponseHeader("Content-Length"); 132 } catch (e) { 133 var contentLength = NaN; 134 } 135 136 // Call the progress handler with what we've got. 137 client.userhandler.onProgress( 138 client.request.responseText, 139 contentLength 140 ); 141 142 } catch (e) { /* Handler method not defined. */ } 143 break; 144 145 // Download complete. 146 case 4: 147 try { 148 client.userhandler.onLoad(client.request.responseText); 149 } catch (e) { 150 /* Handler method not defined. */ 151 } finally { 152 // Call no longer in progress. 153 client.callInProgress = false; 154 } 155 break; 156 } 157 } 158 159 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |