Created
July 22, 2016 02:15
-
-
Save bwiggs/6b8e068cf657f52a68075b76d77558d3 to your computer and use it in GitHub Desktop.
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
window.addEventListener('load', function() { | |
var githubBtn = document.getElementById('github'); | |
var otherBtn = document.getElementById('other'); | |
otherBtn.addEventListener('click', function() { | |
getUrl('http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=robots'); | |
}); | |
githubBtn.addEventListener('click', function() { | |
getUrl('https://api.github.com/users/bwiggs'); | |
}); | |
function getUrl(url) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = handleXHR; | |
xhr.open('GET', url); | |
xhr.send(); | |
} | |
function handleXHR() { | |
if(this.readyState === XMLHttpRequest.DONE) { | |
switch(this.status) { | |
case 200: | |
console.log(this.responseText); | |
break; | |
default: | |
console.log(this.status, 'error'); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment