Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created June 20, 2011 18:38
Show Gist options
  • Save 3rd-Eden/1036246 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/1036246 to your computer and use it in GitHub Desktop.
XHR request factory, only does the feature detection once and generates a dedicated function
util.request = (function(){
var fn = {
body: 'return null'
};
if ('XDomainRequest' in window){
fn.body = 'return new XDomainRequest();';
fn.xdomain = true;
} else if ('XMLHttpRequest' in window) {
fn.body = 'return new XMLHttpRequest();';
fn.xdomain = util.ua.hasCORS;
} else if ('ActiveXObject' in window) {
try {
new ActiveXObject('MSXML2.XMLHTTP');
fn.body = 'return new ActiveXObject("MSXML2.XMLHTTP");';
} catch(e) {
try {
new ActiveXObject('Microsoft.XMLHTTP');
fn.body = 'return new ActiveXObject("Microsoft.XMLHTTP");';
} catch (e) {}
}
}
return new Function('xdomain', fn.xdomain ? fn.body : 'if(xdomain)return null;' + fn.body);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment