Skip to content
Closed
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
File renamed without changes.
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>
44 changes: 44 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function fetchComic() {
const container = document.getElementById("comic-container");

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);

// CLEAR OLD CONTENT
container.innerHTML = "";

// SAFE ELEMENT CREATION (FIXES XSS)
const title = document.createElement("h2");
title.textContent = data.title;

const img = document.createElement("img");
img.src = data.img;
img.alt = data.alt;

const desc = document.createElement("p");
desc.textContent = data.alt;

container.appendChild(title);
container.appendChild(img);
container.appendChild(desc);
})
.catch(error => {
container.innerHTML = "";

const errorMsg = document.createElement("p");
errorMsg.textContent = "Error loading comic 😢";

container.appendChild(errorMsg);

console.error(error);
});
}

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