curl-amd
Version:
curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
53 lines (46 loc) • 1.21 kB
JavaScript
define(/*=='curl/plugin/_fetchText',==*/ [], function () {
var xhr, progIds;
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
xhr = function () {
if (typeof XMLHttpRequest !== "undefined") {
// rewrite the getXhr method to always return the native implementation
xhr = function () {
return new XMLHttpRequest();
};
}
else {
// keep trying progIds until we find the correct one, then rewrite the getXhr method
// to always return that one.
var noXhr = xhr = function () {
throw new Error("getXhr(): XMLHttpRequest not available");
};
while (progIds.length > 0 && xhr === noXhr) (function (id) {
try {
new ActiveXObject(id);
xhr = function () {
return new ActiveXObject(id);
};
}
catch (ex) {
}
}(progIds.shift()));
}
return xhr();
};
function fetchText (url, callback, errback) {
var x = xhr();
x.open('GET', url, true);
x.onreadystatechange = function (e) {
if (x.readyState === 4) {
if (x.status < 400) {
callback(x.responseText);
}
else {
errback(new Error('fetchText() failed. status: ' + x.statusText));
}
}
};
x.send(null);
}
return fetchText;
});