Created
December 21, 2018 14:25
-
-
Save cvengler/d3b9b928d83fe9f16fa8aadc7a902bb2 to your computer and use it in GitHub Desktop.
Display a random cat on a website using random.cat
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
// Create an image in HTML with the ID "catimg" | |
document.getElementById("catimg").style.display = "none"; | |
function getcat() { | |
let rURL = "http://aws.random.cat/meow"; | |
let r = new XMLHttpRequest(); | |
r.open("GET", rURL); | |
r.responseType = "json"; | |
r.send(); | |
r.onload = function() { | |
let jsonObj = r.response; | |
let img = document.getElementById("catimg"); | |
img.src = jsonObj["file"]; | |
img.style.display = "inline-block"; | |
} | |
} | |
getcat(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment