Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 26, 2011 17:39
Show Gist options
  • Select an option

  • Save jed/993585 to your computer and use it in GitHub Desktop.

Select an option

Save jed/993585 to your computer and use it in GitHub Desktop.
get a supported XHR instance
function(
a // cursor placeholder
){
for( // for all a
a = 3; // from 3
a--; // to 0,
) try { // try
return new( // returning a new
this.XMLHttpRequest // XMLHttpRequest (w3c)
|| // or
ActiveXObject // ActiveXObject (MS)
)([ // reflecting
"Msxml2", // the
"Msxml3", // various
"Microsoft"][a] + // MS flavors
".XMLHTTP" // and appropriate suffix,
)
}
catch(e){} // and ignore when it fails.
}
function(a){for(a=3;a--;)try{return new(this.XMLHttpRequest||ActiveXObject)(["Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP")}catch(e){}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
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": "getXHR",
"description": "get a supported XHR instance",
"keywords": [
"ajax",
"XHR",
"cross-browser",
"XmlHttpRequest"
]
}
@tsaniel

tsaniel commented Jul 11, 2011

Copy link
Copy Markdown
function(a){for(a=3;a--;)try{return new(this.XMLHttpRequest||ActiveXObject)(["Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP")}catch(e){}}

My solution with saving 3 bytes.

@jed

jed commented Jul 12, 2011

Copy link
Copy Markdown
Author

interesting... are you sure this won't interfere with normal operation of XMLHttpRequest?

@tsaniel

tsaniel commented Jul 12, 2011

Copy link
Copy Markdown

I am not sure but I think it won't because XMLHttpRequest.length returns 0 (just ignore the arguments object). Actually it seems working well.

@jed

jed commented Jul 12, 2011

Copy link
Copy Markdown
Author

is the this needed for some reason?

@tsaniel

tsaniel commented Jul 13, 2011

Copy link
Copy Markdown

It is needed. If the XMLHttpRequest doesn't exist in the global object, the browsers will throw an error, but this.XMLHttpRequest(the this here is actually window) will be undefined instead and go to ActiveXObject.

@jed

jed commented Jul 13, 2011

Copy link
Copy Markdown
Author

awesome, i've reflected the change. thanks @tsaniel!

@atk

atk commented Jul 13, 2011

Copy link
Copy Markdown

Just for the records, is there any IE version without a native XMLHttpRequest and support for Microsoft.XMLHTTP?

@tsaniel

tsaniel commented Jul 13, 2011

Copy link
Copy Markdown

It's quite confusing indeed. According to http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx#_id , I think we can simply use new ActiveXObject('MSXML2.XMLHTTP.3.0').

@jed

jed commented Jul 13, 2011

Copy link
Copy Markdown
Author

yikes, really? since this code was ported from PPK, i'm inclined to think it's good enough for now.

@tsaniel

tsaniel commented Jul 13, 2011

Copy link
Copy Markdown

So just keep the code unchanged... until someone finds another better solution.

@atk

atk commented Jul 13, 2011

Copy link
Copy Markdown

So instead of the current function, we could simply use

(function(){try{return new(this.XMLHttpRequest||ActiveXObject)('MSXML2.XMLHTTP.3.0')}catch(e){}}

@yckart

yckart commented Jul 29, 2013

Copy link
Copy Markdown

@atk It should be:

(function(){try{return new(this.XMLHttpRequest||ActiveXObject)('MSXML2.XMLHTTP.3.0')}catch(e){}}());

@xeoncross

Copy link
Copy Markdown

I took the liberty to extend the code just a bit more (even if it isn't 140bytes anymore).

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