Created
June 20, 2011 18:38
-
-
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
This file contains hidden or 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
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