Skip to content

Instantly share code, notes, and snippets.

@subzey
Forked from 140bytes/LICENSE.txt
Created May 25, 2011 19:31
Show Gist options
  • Save subzey/991713 to your computer and use it in GitHub Desktop.
Save subzey/991713 to your computer and use it in GitHub Desktop.
140byt.es Cross-browser XHR
/* arguments are defined but ignored */
function(s, a){
/*
Here:
a = "Msxml2.XMLHTTP"
((anonymous array)) = ["Msxml2.XMLHTTP"]
((anonymous array)) = ["Msxml2.XMLHTTP", "Msxml2.XMLHTTP.3.0"]
((anonymous array)) = ["Msxml2.XMLHTTP", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP.6.0"]
a = ((that anonymous array))
*/
a = [a = "Msxml2.XMLHTTP", a + ".3.0", a + ".6.0"];
do /* try..catch do not requires {} */
try {
/*
Get the last element, so during the loop s variable is:
"Msxml2.XMLHTTP.6.0"
"Msxml2.XMLHTTP.3.0"
"Msxml2.XMLHTTP"
undefined
*/
s=a.pop();
/* try to return new... */ return new /* ... XHR if s==undefined (last iteration), AX if not */ (s ? ActiveXObject : XMLHttpRequest) /*... using s itself as an argument */(s)
} catch(e){
/* if it is failed do nothing and leave ; for very old version of Firefox */;
}
/* if s == undefined (and a.length == 0) that was a last iteration and exit */
while(s)
/* thats it, return undefined if all previous return attempts failed */
}
function(s,a){a=[a="Msxml2.XMLHTTP",a+".3.0",a+".6.0"];do try{s=a.pop();return new(s?ActiveXObject:XMLHttpRequest)(s)}catch(e){;}while(s)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "crossbrowser_XHR",
"description": "Function creates XMLHTTPRequest object in IE6+ and other browsers.",
"keywords": [
"xhr"
]
}
@jed
Copy link

jed commented May 26, 2011

is this reliable? closest thing i've found is this.

@subzey
Copy link
Author

subzey commented May 27, 2011

It is tested in IE 6..9, Firefox, Opera, Chrome.
(IE6 is tested 3 times on different PCs)

Also, this should work in any future browser that implements XMLHttpRequest constructor.

@jed
Copy link

jed commented May 27, 2011

awesome work, this will definitely come in handy.

@jed
Copy link

jed commented Jul 11, 2011

hey @subzey, could you take the comments out of your package.json?

@atk
Copy link

atk commented Jul 11, 2011

catch(e){;} - the semicolon is superfluous here - and do while can be replaced with a for loop:

function(s,a){for(a=[a="Msxml2.XMLHTTP",a+".3.0",a+".6.0"];;)try{s=a.pop();return new(s?ActiveXObject:XMLHttpRequest)(s)}catch(e){}}

@jdalton
Copy link

jdalton commented Aug 21, 2011

The use of various ActiveX progIDs is pretty misunderstood.
You can check out this rad 3 page thread on the topic (read through all pages, it's a bit old but does the job).
To be honest I don't know the benefit of using 6.0 over 3.0 vs plain MSXML2.XMLHTTP, the post does explain some possible differences between MSXML2.XMLHTTPand Microsoft.XMLHTTP though.

In practice jQuery seems to be doing fine with just using Microsoft.XMLHTTP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment