Last active
May 31, 2025 11:50
-
-
Save smart-onion/42576a939979d271a1a5024a5abde275 to your computer and use it in GitHub Desktop.
JS9
This file contains hidden or 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
<!doctype html> | |
<html class="no-js" lang=""> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title></title> | |
<link rel="stylesheet" href="css/style.css"> | |
<meta name="description" content=""> | |
<meta property="og:title" content=""> | |
<meta property="og:type" content=""> | |
<meta property="og:url" content=""> | |
<meta property="og:image" content=""> | |
<meta property="og:image:alt" content=""> | |
<link rel="icon" href="/favicon.ico" sizes="any"> | |
<link rel="icon" href="/icon.svg" type="image/svg+xml"> | |
<link rel="apple-touch-icon" href="icon.png"> | |
<link rel="manifest" href="site.webmanifest"> | |
<meta name="theme-color" content="#fafafa"> | |
</head> | |
<style> | |
body { | |
background-color: #202845; | |
} | |
#welcome{ | |
border: 3px solid #46578e; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
} | |
#welcome pre{ | |
color: white; | |
font-size: 20px; | |
} | |
#input{ | |
margin-top: 20px; | |
border: 3px solid #46578e; | |
padding: 10px; | |
} | |
#input input{ | |
background-color: #46578e; | |
border: 3px solid #46578e; | |
} | |
#input input::placeholder{ | |
color: white; | |
} | |
#button{ | |
background-color: #46578e; | |
color: white; | |
border: 3px solid #46578e; | |
} | |
#button:hover{ | |
background-color: #6178bf; | |
} | |
#content{ | |
border: 3px solid #46578e; | |
margin-top: 20px; | |
display: flex; | |
flex-direction: row; | |
align-content: center; | |
padding: 5px; | |
word-break: break-all | |
} | |
#image{ | |
width: 480px; | |
height: 240px; | |
} | |
#fact{ | |
color: white; | |
font-size: 20px; | |
word-break: break-all | |
} | |
</style> | |
<style></style> | |
<body> | |
<div id="welcome"> | |
<pre>Welcome to cats world!</pre> | |
</div> | |
<div id="input"> | |
<input type="text" id="input-field" placeholder="Enter length of fact"/> | |
<button id="button">Get Fact</button> | |
</div> | |
<div id="content"> | |
<img id="image" src="" alt=""> | |
<pre id="fact"></pre> | |
</div> | |
<div id="items"></div> | |
</body> | |
<script> | |
//APIs | |
// https://catfact.ninja/fact?max_length=140 | |
// https://api.thecatapi.com/v1/images/search | |
function getFact(length){ | |
let xhr = new XMLHttpRequest() | |
xhr.open('GET', `https://catfact.ninja/fact?max_length=${length}`, true) | |
xhr.onload = function(){ | |
if(xhr.status === 200){ | |
let data = JSON.parse(xhr.responseText) | |
document.getElementById('fact').innerText = data.fact | |
} | |
else{ | |
document.getElementById('fact').innerText = "OOPS!! Looks like no cats facts found" | |
} | |
} | |
xhr.send() | |
} | |
function getImage(){ | |
let xhr = new XMLHttpRequest() | |
xhr.open('GET', `https://api.thecatapi.com/v1/images/search`, true) | |
xhr.onload = function(){ | |
if(xhr.status === 200){ | |
let data = JSON.parse(xhr.responseText) | |
console.log(data) | |
document.getElementById('image').src = data[0].url; | |
} | |
else{ | |
document.getElementById('fact').alt = "OOPS!! Looks like no cats photo found" | |
} | |
} | |
xhr.send() | |
} | |
getImage() | |
getFact(140) | |
document.getElementById('button').onclick = function(){ | |
let length = document.getElementById('input-field').value | |
getFact(length) | |
getImage() | |
} | |
</script> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment