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
18 changes: 18 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Programmer Humour</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<h1>Latest XKCD Comic</h1>

<div id="comic-container">
<p id="loading">Loading comic...</p>
</div>

<script src="script.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function fetchComic() {
const container = document.getElementById("comic-container");
const loading = document.getElementById("loading");

fetch("https://xkcd.now.sh/?comic=latest")
.then(response => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then(data => {
console.log(data);

if (loading) loading.remove();

container.innerHTML = `
<h2>${data.title}</h2>
<img src="${data.img}" alt="${data.alt}" />
<p>${data.alt}</p>
`;
})
.catch(error => {
console.error(error);

if (loading) {
loading.textContent = "Error loading comic 😢";
} else {
container.innerHTML = `<p>Error loading comic 😢</p>`;
}
});
}

window.addEventListener("load", fetchComic);
11 changes: 11 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
}

img {
max-width: 100%;
height: auto;
margin-top: 20px;
}
Loading