Skip to content

Instantly share code, notes, and snippets.

@N8-B
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save N8-B/fe8aa4c3ccb3692ba6e6 to your computer and use it in GitHub Desktop.
Save N8-B/fe8aa4c3ccb3692ba6e6 to your computer and use it in GitHub Desktop.
Create XHR function
// Function for creating an XHR object which is compatible in all browsers.
// This implementation is compatible with the original Internet Explorer version.
// So, it can be used to create an XHR object in any browser.
// Example usage:
// var xhr = createXHR();
// xhr.onreadystatechange = function() {
// if(xhr.readyState == 4) {
// if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
// alert(xhr.responseText);
// } else {
// alert("Request was unsuccessful: " + xhr.status);
// }
// }
// };
// xhr.open("GET", "data.json", true);
// xhr.send(null);
function createXHR() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
if (typeof arguments.callee.AtiveXString != "string") {
var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.Http"],
i,
len;
for(i = 0, len = versions.length; i < len; i++) {
try {
new ActiveXObject(versions[i]);
arguments.callee.ActiveXString = versions[i];
break;
}
catch(ex) {
// skip
}
}
}
} else {
throw new Error("No XHR object available.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment