forked from CodeYourFuture/Module-Data-Flows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (24 loc) · 801 Bytes
/
script.js
File metadata and controls
33 lines (24 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const API_URL = "https://xkcd.now.sh/?comic=latest";
async function getLatestComic() {
const container = document.getElementById("comic-container");
try {
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
console.log(data);
if (!data.img) {
throw new Error("API did not return an image");
}
container.innerHTML = "";
const img = document.createElement("img");
img.src = data.img;
img.alt = data.alt || data.title || "XKCD comic";
container.appendChild(img);
} catch (error) {
console.error("Fetch error:", error);
container.innerHTML = `<p>Could not load the comic. Please try again later.</p>`;
}
}
getLatestComic();