Skip to content

Instantly share code, notes, and snippets.

@jesgundy
Created September 13, 2012 15:08
Show Gist options
  • Save jesgundy/3714956 to your computer and use it in GitHub Desktop.
Save jesgundy/3714956 to your computer and use it in GitHub Desktop.
Vanilla JS getHTTPObject AJAX request
// Reusable x-browser XMLHttpRequest
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xhr = false;
}
}
}
return xhr;
}
function grabFile(file) {
var request = getHTTPObject();
//GET
if (request) {
request.open('GET', file, true);
request.send(null);
request.onreadystatechange = function(){
if (request.readyState != 4) return false;
if (request.status == 200 || request.status == 304) {
console.log(request.responseText);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment