Skip to content

Instantly share code, notes, and snippets.

@twolfe2
Last active May 24, 2016 01:30
Show Gist options
  • Save twolfe2/e7efc0c674d79f4add8c628646205132 to your computer and use it in GitHub Desktop.
Save twolfe2/e7efc0c674d79f4add8c628646205132 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script>
//step 1
var request = new XMLHttpRequest();
//step 2, create the callback funciton
request.onreadystatechange = function() {
//4 means the request is complete
if (request.readystate === 4) {
//200 indicates there where no errors with the request
if (request.status === 200) {
//addMe.html will be added after Hello
document.getElementById('addHere').innerHTML = request.response();
}
//if there is an error log it to the console (CMD+Option+J)
else {
console.log(request.statusText);
}
}
}
//step 3, get request for the addMe.html file
request.open('GET', 'addMe.html');
//add an event listener to the button, when the user clicks the button the request will be sent
document.getElementById("initiate").addEventListener("click", function() {
//step 4 send the request
request.send();
//hide the button
doucment.getElementById('initiate').style.display = "none";
});
</script>
</head>
<body>
<div>
<h1>Hello <span id="addHere"></span></h1>
<button id="initiate">Show me the AJAX!</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment