Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>XKCD Comics</title>
</head>
<body>
<header>
<h1>Latest XKCD Comic</h1>
</header>

<main>
<!-- The comic image goes here -->
<img id="comic-image" />
<!-- comic image alt text goes here -->
<div id="comic-alt"></div>
</main>
<script src="script.js"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const comicImage = document.getElementById("comic-image");
const comicAlt = document.getElementById("comic-alt");

function fetchComic() {
fetch("https://xkcd.now.sh/?comic=latest")
.then((response) => {
if (!response.ok) {
// Error handeling
throw new Error("Network fetch was not ok");
}
return response.json();
})
.then((data) => {
console.log(data);
comicImage.src = data.img;
comicImage.alt = data.alt;
comicAlt.textContent = data.alt;
})
.catch((error) => {
// Error handeling
console.error("Fetch error:", error);
alert("Failed to load comic. Check console for details."); // Extra error handeling popup on screen
});
}

fetchComic();
25 changes: 25 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
body {
background-color: antiquewhite;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

h1 {
text-align: center;
}

img {
display: block;
height: auto;
border: solid 2px black;
}

#comic-alt {
margin-top: 16px;
font-style: italic;
color: black;
text-align: center;
font-size: 1.4rem;
}