Skip to content

Instantly share code, notes, and snippets.

@cvengler
Created December 21, 2018 14:25
Show Gist options
  • Save cvengler/d3b9b928d83fe9f16fa8aadc7a902bb2 to your computer and use it in GitHub Desktop.
Save cvengler/d3b9b928d83fe9f16fa8aadc7a902bb2 to your computer and use it in GitHub Desktop.
Display a random cat on a website using random.cat
// 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