您好,欢迎访问一九零五行业门户网

一份老外写的XMLHttpRequest代码多浏览器支持兼容性_javascript技巧

这几天要构思用javascript调用asp.net的webservice,需要到xmlhttp来支持,但发现opera的xmlhttprequest很烂,实在支持不下去,后来到处找,终于发现这份代码,在opera中是利用java.net.url等类来实现的,不敢独享,特发上来与大家同乐。
复制代码 代码如下:
/*
cross-browser xmlhttprequest v1.2
=================================
emulate gecko 'xmlhttprequest()' functionality in ie and opera. opera requires
the sun java runtime environment .
by andrew gregory
http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/
this work is licensed under the creative commons attribution license. to view a
copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or
send a letter to creative commons, 559 nathan abbott way, stanford, california
94305, usa.
attribution: leave my name and web address in this script intact.
not supported in opera
----------------------
* user/password authentication
* responsexml data member
not fully supported in opera
----------------------------
* async requests
* abort()
* getallresponseheaders(), getallresponseheader(header)
*/
// ie support
if (window.activexobject && !window.xmlhttprequest) {
  window.xmlhttprequest = function() {
    var msxmls = new array(
      'msxml2.xmlhttp.5.0',
      'msxml2.xmlhttp.4.0',
      'msxml2.xmlhttp.3.0',
      'msxml2.xmlhttp',
      'microsoft.xmlhttp');
    for (var i = 0; i       try {
        return new activexobject(msxmls[i]);
      } catch (e) {
      }
    }
    return null;
  };
}
// gecko support
/* ;-) */
// opera support
if (window.opera && !window.xmlhttprequest) {
  window.xmlhttprequest = function() {
    this.readystate = 0; // 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete
    this.status = 0; // http status codes
    this.statustext = '';
    this._headers = [];
    this._aborted = false;
    this._async = true;
    this._defaultcharset = 'iso-8859-1';
    this._getcharset = function() {
      var charset = _defaultcharset;
      var contenttype = this.getresponseheader('content-type').touppercase();
      val = contenttype.indexof('charset=');
      if (val != -1) {
        charset = contenttype.substring(val);
      }
      val = charset.indexof(';');
      if (val != -1) {
        charset = charset.substring(0, val);
      }
      val = charset.indexof(',');
      if (val != -1) {
        charset = charset.substring(0, val);
      }
      return charset;
    };
    this.abort = function() {
      this._aborted = true;
    };
    this.getallresponseheaders = function() {
      return this.getallresponseheader('*');
    };
    this.getallresponseheader = function(header) {
      var ret = '';
      for (var i = 0; i         if (header == '*' || this._headers[i].h == header) {
          ret += this._headers[i].h + ': ' + this._headers[i].v + '\n';
        }
      }
      return ret;
    };
    this.getresponseheader = function(header) {
      var ret = getallresponseheader(header);
      var i = ret.indexof('\n');
      if (i != -1) {
        ret = ret.substring(0, i);
      }
      return ret;
    };
    this.setrequestheader = function(header, value) {
      this._headers[this._headers.length] = {h:header, v:value};
    };
    this.open = function(method, url, async, user, password) {
      this.method = method;
      this.url = url;
      this._async = true;
      this._aborted = false;
      this._headers = [];
      if (arguments.length >= 3) {
        this._async = async;
      }
      if (arguments.length > 3) {
        opera.posterror('xmlhttprequest.open() - user/password not supported');
      }
      this.readystate = 1;
      if (this.onreadystatechange) {
        this.onreadystatechange();
      }
    };
    this.send = function(data) {
      if (!navigator.javaenabled()) {
        alert(xmlhttprequest.send() - java must be installed and enabled.);
        return;
      }
      if (this._async) {
        settimeout(this._sendasync, 0, this, data);
        // this is not really asynchronous and won't execute until the current
        // execution context ends
      } else {
        this._sendsync(data);
      }
    }
    this._sendasync = function(req, data) {
      if (!req._aborted) {
        req._sendsync(data);
      }
    };
    this._sendsync = function(data) {
      this.readystate = 2;
      if (this.onreadystatechange) {
        this.onreadystatechange();
      }
      // open connection
      var url = new java.net.url(new java.net.url(window.location.href), this.url);
      var conn = url.openconnection();
      for (var i = 0; i         conn.setrequestproperty(this._headers[i].h, this._headers[i].v);
      }
      this._headers = [];
      if (this.method == 'post') {
        // post data
        conn.setdooutput(true);
        var wr = new java.io.outputstreamwriter(conn.getoutputstream(), this._getcharset());
        wr.write(data);
        wr.flush();
        wr.close();
      }
      // read response headers
      // note: the getheaderfield() methods always return nulls for me :(
      var gotcontentencoding = false;
      var gotcontentlength = false;
      var gotcontenttype = false;
      var gotdate = false;
      var gotexpiration = false;
      var gotlastmodified = false;
      for (var i = 0; ; i++) {
        var hdrname = conn.getheaderfieldkey(i);
        var hdrvalue = conn.getheaderfield(i);
        if (hdrname == null && hdrvalue == null) {
          break;
        }
        if (hdrname != null) {
          this._headers[this._headers.length] = {h:hdrname, v:hdrvalue};
          switch (hdrname.tolowercase()) {
            case 'content-encoding': gotcontentencoding = true; break;
            case 'content-length'  : gotcontentlength   = true; break;
            case 'content-type'    : gotcontenttype     = true; break;
            case 'date'            : gotdate            = true; break;
            case 'expires'         : gotexpiration      = true; break;
            case 'last-modified'   : gotlastmodified    = true; break;
          }
        }
      }
      // try to fill in any missing header information
      var val;
      val = conn.getcontentencoding();
      if (val != null && !gotcontentencoding) this._headers[this._headers.length] = {h:'content-encoding', v:val};
      val = conn.getcontentlength();
      if (val != -1 && !gotcontentlength) this._headers[this._headers.length] = {h:'content-length', v:val};
      val = conn.getcontenttype();
      if (val != null && !gotcontenttype) this._headers[this._headers.length] = {h:'content-type', v:val};
      val = conn.getdate();
      if (val != 0 && !gotdate) this._headers[this._headers.length] = {h:'date', v:(new date(val)).toutcstring()};
      val = conn.getexpiration();
      if (val != 0 && !gotexpiration) this._headers[this._headers.length] = {h:'expires', v:(new date(val)).toutcstring()};
      val = conn.getlastmodified();
      if (val != 0 && !gotlastmodified) this._headers[this._headers.length] = {h:'last-modified', v:(new date(val)).toutcstring()};
      // read response data
      var reqdata = '';
      var stream = conn.getinputstream();
      if (stream) {
        var reader = new java.io.bufferedreader(new java.io.inputstreamreader(stream, this._getcharset()));
        var line;
        while ((line = reader.readline()) != null) {
          if (this.readystate == 2) {
            this.readystate = 3;
            if (this.onreadystatechange) {
              this.onreadystatechange();
            }
          }
          reqdata += line + '\n';
        }
        reader.close();
        this.status = 200;
        this.statustext = 'ok';
        this.responsetext = reqdata;
        this.readystate = 4;
        if (this.onreadystatechange) {
          this.onreadystatechange();
        }
        if (this.onload) {
          this.onload();
        }
      } else {
        // error
        this.status = 404;
        this.statustext = 'not found';
        this.responsetext = '';
        this.readystate = 4;
        if (this.onreadystatechange) {
          this.onreadystatechange();
        }
        if (this.onerror) {
          this.onerror();
        }
      }
    };
  };
}
// activexobject emulation
if (!window.activexobject && window.xmlhttprequest) {
  window.activexobject = function(type) {
    switch (type.tolowercase()) {
      case 'microsoft.xmlhttp':
      case 'msxml2.xmlhttp':
      case 'msxml2.xmlhttp.3.0':
      case 'msxml2.xmlhttp.4.0':
      case 'msxml2.xmlhttp.5.0':
        return new xmlhttprequest();
    }
    return null;
  };
}
其它类似信息

推荐信息