Skip to content

Instantly share code, notes, and snippets.

@mzabriskie
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save mzabriskie/60c0e30b00e1ab875e16 to your computer and use it in GitHub Desktop.

Select an option

Save mzabriskie/60c0e30b00e1ab875e16 to your computer and use it in GitHub Desktop.
Creating cross browser XHR object
function createXHR() {
if (!createXHR.__memoize) {
var factory = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Microsoft.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
];
var xhr;
for (var i=0, l=factory.length; i<l; i++) {
try { xhr = factory[i](); } catch (e) { continue; }
createXHR.__memoize = factory[i];
break;
}
return xhr;
}
return createXHR.__memoize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment