Skip to content

Instantly share code, notes, and snippets.

@gabrielgilini
Created September 22, 2011 13:53
Show Gist options
  • Save gabrielgilini/1234821 to your computer and use it in GitHub Desktop.
Save gabrielgilini/1234821 to your computer and use it in GitHub Desktop.
Simple XMLHttpRequest example
(function()
{
var global = this;
var createXmlHttpRequest = (function()
{
var i,
fs = [// for legacy eg. IE 5
function()
{
return new global.ActiveXObject("Microsoft.XMLHTTP");
},
// for fully patched Win2k SP4 and up
function()
{
return new global.ActiveXObject("Msxml2.XMLHTTP.3.0");
},
// IE 6 users that have updated their msxml dll files.
function()
{
return new global.ActiveXObject("Msxml2.XMLHTTP.6.0");
},
// IE7, Safari, Mozilla, Opera, etc (NOTE: IE7+ native version does not support overrideMimeType or local file requests)
function()
{
return new global.XMLHttpRequest();
}];
// Loop through the possible factories to try and find one that
// can instantiate an XMLHttpRequest object that works.
for (i=fs.length; i--; )
{
try
{
if (fs[i]())
{
return fs[i];
}
}
catch (e) {}
}
})();
var xhr = createXmlHttpRequest();
xhr.open("GET", "resource/url/", true);
xhr.onreadystatechange = function()
{
if (xhr.readyState==4)
{
console.log(xhr.responseText);
}
}
xhr.send(null);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment