Created
August 29, 2013 19:55
-
-
Save ajace/6382658 to your computer and use it in GitHub Desktop.
Javascript ajax snippet from Mozilla
This file contains hidden or 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
(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