Created
September 13, 2012 15:08
-
-
Save jesgundy/3714956 to your computer and use it in GitHub Desktop.
Vanilla JS getHTTPObject AJAX request
This file contains 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
// 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