梅开二度 激情超越

~~常用激扬的句子去激励自己的学生,为什么自己却莫名的沉沦了!~~

网上找到的一个 Cross-Browser XMLHttpRequest 实现

今天从网上找到的一个 Cross-Browser XMLHttpRequest 实现,感觉写的挺好的,收藏一下。
兼容 IE, Mozilla/Firefox 和 Opera

js 代码
 
  1. /* 
  2. Cross-Browser XMLHttpRequest v1.2 
  3. ================================= 
  4. Emulate Gecko 'XMLHttpRequest()' functionality in IE and Opera. Opera requires 
  5. the Sun Java Runtime Environment  
  6. by Andrew Gregory 
  7. http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/ 
  8. This work is licensed under the Creative Commons Attribution License. To view a 
  9. copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or 
  10. send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 
  11. 94305, USA. 
  12. Attribution: Leave my name and web address in this script intact. 
  13. Not Supported in Opera 
  14. ---------------------- 
  15. * user/password authentication 
  16. * responseXML data member 
  17. Not Fully Supported in Opera 
  18. ---------------------------- 
  19. * async requests 
  20. * abort() 
  21. * getAllResponseHeaders(), getAllResponseHeader(header) 
  22. */  
  23. // IE support  
  24. if (window.ActiveXObject && !window.XMLHttpRequest) {  
  25.   window.XMLHttpRequest = function() {  
  26.     var msxmls = new Array(  
  27.       'Msxml2.XMLHTTP.5.0',  
  28.       'Msxml2.XMLHTTP.4.0',  
  29.       'Msxml2.XMLHTTP.3.0',  
  30.       'Msxml2.XMLHTTP',  
  31.       'Microsoft.XMLHTTP');  
  32.     for (var i = 0; i < msxmls.length; i++) {  
  33.       try {  
  34.         return new ActiveXObject(msxmls[i]);  
  35.       } catch (e) {  
  36.       }  
  37.     }  
  38.     return null;  
  39.   };  
  40. }  
  41. // Gecko support  
  42. /* ;-) */  
  43. // Opera support  
  44. if (window.opera && !window.XMLHttpRequest) {  
  45.   window.XMLHttpRequest = function() {  
  46.     this.readyState = 0; // 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete  
  47.     this.status = 0; // HTTP status codes  
  48.     this.statusText = '';  
  49.     this._headers = [];  
  50.     this._aborted = false;  
  51.     this._async = true;  
  52.     this._defaultCharset = 'ISO-8859-1';  
  53.     this._getCharset = function() {  
  54.       var charset = _defaultCharset;  
  55.       var contentType = this.getResponseHeader('Content-type').toUpperCase();  
  56.       val = contentType.indexOf('CHARSET=');  
  57.       if (val != -1) {  
  58.         charset = contentType.substring(val);  
  59.       }  
  60.       val = charset.indexOf(';');  
  61.       if (val != -1) {  
  62.         charset = charset.substring(0, val);  
  63.       }  
  64.       val = charset.indexOf(',');  
  65.       if (val != -1) {  
  66.         charset = charset.substring(0, val);  
  67.       }  
  68.       return charset;  
  69.     };  
  70.     this.abort = function() {  
  71.       this._aborted = true;  
  72.     };  
  73.     this.getAllResponseHeaders = function() {  
  74.       return this.getAllResponseHeader('*');  
  75.     };  
  76.     this.getAllResponseHeader = function(header) {  
  77.       var ret = '';  
  78.       for (var i = 0; i < this._headers.length; i++) {  
  79.         if (header == '*' || this._headers[i].h == header) {  
  80.           ret += this._headers[i].h + ': ' + this._headers[i].v + '\n';  
  81.         }  
  82.       }  
  83.       return ret;  
  84.     };  
  85.     this.getResponseHeader = function(header) {  
  86.       var ret = getAllResponseHeader(header);  
  87.       var i = ret.indexOf('\n');  
  88.       if (i != -1) {  
  89.         ret = ret.substring(0, i);  
  90.       }  
  91.       return ret;  
  92.     };  
  93.     this.setRequestHeader = function(header, value) {  
  94.       this._headers[this._headers.length] = {h:header, v:value};  
  95.     };  
  96.     this.open = function(method, url, async, user, password) {  
  97.       this.method = method;  
  98.       this.url = url;  
  99.       this._async = true;  
  100.       this._aborted = false;  
  101.       this._headers = [];  
  102.       if (arguments.length >= 3) {  
  103.         this._async = async;  
  104.       }  
  105.       if (arguments.length > 3) {  
  106.         opera.postError('XMLHttpRequest.open() - user/password not supported');  
  107.       }  
  108.       this.readyState = 1;  
  109.       if (this.onreadystatechange) {  
  110.         this.onreadystatechange();  
  111.       }  
  112.     };  
  113.     this.send = function(data) {  
  114.       if (!navigator.javaEnabled()) {  
  115.         alert("XMLHttpRequest.send() - Java must be installed and enabled.");  
  116.         return;  
  117.       }  
  118.       if (this._async) {  
  119.         setTimeout(this._sendasync, 0, this, data);  
  120.         // this is not really asynchronous and won't execute until the current  
  121.         // execution context ends  
  122.       } else {  
  123.         this._sendsync(data);  
  124.       }  
  125.     }  
  126.     this._sendasync = function(req, data) {  
  127.       if (!req._aborted) {  
  128.         req._sendsync(data);  
  129.       }  
  130.     };  
  131.     this._sendsync = function(data) {  
  132.       this.readyState = 2;  
  133.       if (this.onreadystatechange) {  
  134.         this.onreadystatechange();  
  135.       }  
  136.       // open connection  
  137.       var url = new java.net.URL(new java.net.URL(window.location.href), this.url);  
  138.       var conn = url.openConnection();  
  139.       for (var i = 0; i < this._headers.length; i++) {  
  140.         conn.setRequestProperty(this._headers[i].h, this._headers[i].v);  
  141.       }  
  142.       this._headers = [];  
  143.       if (this.method == 'POST') { 
  144.         // POST data 
  145.         conn.setDoOutput(true); 
  146.         var wr = new java.io.OutputStreamWriter(conn.getOutputStream(), this._getCharset()); 
  147.         wr.write(data); 
  148.         wr.flush(); 
  149.         wr.close(); 
  150.       } 
  151.       // read response headers 
  152.       // NOTE: the getHeaderField() methods always return nulls for me :( 
  153.       var gotContentEncoding = false; 
  154.       var gotContentLength = false; 
  155.       var gotContentType = false; 
  156.       var gotDate = false; 
  157.       var gotExpiration = false; 
  158.       var gotLastModified = false; 
  159.       for (var i = 0; ; i++) { 
  160.         var hdrName = conn.getHeaderFieldKey(i); 
  161.         var hdrValue = conn.getHeaderField(i); 
  162.         if (hdrName == null && hdrValue == null) { 
  163.           break; 
  164.         } 
  165.         if (hdrName != null) { 
  166.           this._headers[this._headers.length] = {h:hdrName, v:hdrValue}; 
  167.           switch (hdrName.toLowerCase()) { 
  168.             case 'content-encoding': gotContentEncoding = true; break; 
  169.             case 'content-length'  : gotContentLength   = true; break; 
  170.             case 'content-type'    : gotContentType     = true; break; 
  171.             case 'date'            : gotDate            = true; break; 
  172.             case 'expires'         : gotExpiration      = true; break; 
  173.             case 'last-modified'   : gotLastModified    = true; break; 
  174.           } 
  175.         } 
  176.       } 
  177.       // try to fill in any missing header information 
  178.       var val; 
  179.       val = conn.getContentEncoding(); 
  180.       if (val != null && !gotContentEncoding) this._headers[this._headers.length] = {h:'Content-encoding', v:val}; 
  181.       val = conn.getContentLength(); 
  182.       if (val != -1 && !gotContentLength) this._headers[this._headers.length] = {h:'Content-length', v:val}; 
  183.       val = conn.getContentType(); 
  184.       if (val != null && !gotContentType) this._headers[this._headers.length] = {h:'Content-type', v:val}; 
  185.       val = conn.getDate(); 
  186.       if (val != 0 && !gotDate) this._headers[this._headers.length] = {h:'Date', v:(new Date(val)).toUTCString()}; 
  187.       val = conn.getExpiration(); 
  188.       if (val != 0 && !gotExpiration) this._headers[this._headers.length] = {h:'Expires', v:(new Date(val)).toUTCString()}; 
  189.       val = conn.getLastModified(); 
  190.       if (val != 0 && !gotLastModified) this._headers[this._headers.length] = {h:'Last-modified', v:(new Date(val)).toUTCString()}; 
  191.       // read response data 
  192.       var reqdata = ''; 
  193.       var stream = conn.getInputStream(); 
  194.       if (stream) { 
  195.         var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream, this._getCharset())); 
  196.         var line; 
  197.         while ((line = reader.readLine()) != null) { 
  198.           if (this.readyState == 2) { 
  199.             this.readyState = 3; 
  200.             if (this.onreadystatechange) { 
  201.               this.onreadystatechange(); 
  202.             } 
  203.           } 
  204.           reqdata += line + '\n'; 
  205.         } 
  206.         reader.close(); 
  207.         this.status = 200; 
  208.         this.statusText = 'OK'; 
  209.         this.responseText = reqdata; 
  210.         this.readyState = 4; 
  211.         if (this.onreadystatechange) { 
  212.           this.onreadystatechange(); 
  213.         } 
  214.         if (this.onload) { 
  215.           this.onload(); 
  216.         } 
  217.       } else { 
  218.         // error 
  219.         this.status = 404; 
  220.         this.statusText = 'Not Found'; 
  221.         this.responseText = ''; 
  222.         this.readyState = 4; 
  223.         if (this.onreadystatechange) { 
  224.           this.onreadystatechange(); 
  225.         } 
  226.         if (this.onerror) { 
  227.           this.onerror(); 
  228.         } 
  229.       } 
  230.     }; 
  231.   }; 
  232. } 
  233. // ActiveXObject emulation 
  234. if (!window.ActiveXObject && window.XMLHttpRequest) { 
  235.   window.ActiveXObject = function(type) { 
  236.     switch (type.toLowerCase()) { 
  237.       case 'microsoft.xmlhttp': 
  238.       case 'msxml2.xmlhttp': 
  239.       case 'msxml2.xmlhttp.3.0': 
  240.       case 'msxml2.xmlhttp.4.0': 
  241.       case 'msxml2.xmlhttp.5.0':  
  242.         return new XMLHttpRequest();  
  243.     }  
  244.     return null;  
  245.   };  
  246. }  

posted on 2006-12-08 23:52 梅开二度 激情超越 阅读(76) 评论(0)  编辑 收藏 引用 网摘 所属分类: AJAX


只有注册用户登录后才能发表评论。

My Links

Blog Stats

留言簿(3)

随笔分类(22)

随笔档案(21)

文章分类(48)

文章档案(44)

相册

收藏夹(40)

Favorite site

My Friends

搜索

最新评论