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
16 changes: 16 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XKCD Comic</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="comic">
<h2 id="title"></h2>
<img id="image" alt="">
<p id="alt"></p>
</div>
<script src="script.js"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const title = document.getElementById("title");
const image = document.getElementById("image");
const alt = document.getElementById("alt");

async function getImg() {
const url = "https://xkcd.now.sh/?comic=latest";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const result = await response.json();
title.textContent = result.safe_title;
image.src = result.img;
alt.textContent = result.alt;
console.log(result);
} catch (error) {
console.log(error.message);
}
}
getImg();
9 changes: 9 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#comic {
text-align: center;
max-width: 800px;
margin: 0 auto;
}

#image {
max-width: 100%;
}
Loading