Skip to content

Instantly share code, notes, and snippets.

@ajace
Created August 29, 2013 19:55
Show Gist options
  • Save ajace/6382658 to your computer and use it in GitHub Desktop.
Save ajace/6382658 to your computer and use it in GitHub Desktop.
Javascript ajax snippet from Mozilla
(function() {
document.getElementById...
.onclick = function() { sendHTTPRequest(); };
var httpRequest;
function sendHTTPRequest() {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if (!httpRequest) {
console.log("Can't do ajax");
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('GET', url);
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
httpRequest.responseText
} else {
console.log('There was a problem with the request.');
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment