A foreigner wrote the XMLHttpRequest code multiple browsers support compatibility

Source: Internet
Author: User
Tags date copy creative commons attribution header net return window
Request|xml|xmlhttprequest| Browser

These days to conceive of using JavaScript call asp.net webservice, need to XMLHTTP to support, but found that opera XMLHttpRequest very rotten, really support not go on, and then find everywhere, finally found this code, in opera is the use of java.net.u RL and other classes to achieve, do not dare to enjoy, special hair up with everyone fun.
[Copy this Code] CODE:
/*
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. 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 < msxmls.length; 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 < this._headers.length; 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 isn't 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 < this._headers.length; 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 on 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 (stream, This._getcharset ()) (new Java.io.InputStreamReader);
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 && windows. 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;
};
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.