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
13 changes: 13 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>xkcd</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js" defer></script>
</head>
<body>
<img id="image" />
</body>
</html>
17 changes: 17 additions & 0 deletions fetch/programmer-humour/script.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay, but if there is an error, you need to also throw the error to the client/user, else your program will crash. You can read up how to throw error in Javascript, to handle errors properly.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fetchData = async () => {
const url = `https://xkcd.now.sh/?comic=latest`;
try {
const response = await fetch(url);

if (!response.ok) throw new Error(`HTTP error: ${response.status}`);

const data = await response.json();
const imageEl = document.getElementById("image");
imageEl.src = data.img;
imageEl.alt = data.alt;
} catch (error) {
console.log(`Failed to fetch: ${error}`);
}
};

fetchData();
7 changes: 7 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(117, 67, 5);
}