Last active
August 29, 2015 14:13
-
-
Save N8-B/fe8aa4c3ccb3692ba6e6 to your computer and use it in GitHub Desktop.
Create XHR function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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