Skip to content

Instantly share code, notes, and snippets.

@bwiggs
Created July 22, 2016 02:15
Show Gist options
  • Save bwiggs/6b8e068cf657f52a68075b76d77558d3 to your computer and use it in GitHub Desktop.
Save bwiggs/6b8e068cf657f52a68075b76d77558d3 to your computer and use it in GitHub Desktop.
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