diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 00000000..fdd9557c --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,22 @@ + + + + + + + XKCD Comics + + +
+

Latest XKCD Comic

+
+ +
+ + + +
+
+ + + diff --git a/fetch/programmer-humour/script.js b/fetch/programmer-humour/script.js new file mode 100644 index 00000000..2ff35c33 --- /dev/null +++ b/fetch/programmer-humour/script.js @@ -0,0 +1,26 @@ +const comicImage = document.getElementById("comic-image"); +const comicAlt = document.getElementById("comic-alt"); + +function fetchComic() { + fetch("https://xkcd.now.sh/?comic=latest") + .then((response) => { + if (!response.ok) { + // Error handeling + throw new Error("Network fetch was not ok"); + } + return response.json(); + }) + .then((data) => { + console.log(data); + comicImage.src = data.img; + comicImage.alt = data.alt; + comicAlt.textContent = data.alt; + }) + .catch((error) => { + // Error handeling + console.error("Fetch error:", error); + alert("Failed to load comic. Check console for details."); // Extra error handeling popup on screen + }); +} + +fetchComic(); diff --git a/fetch/programmer-humour/style.css b/fetch/programmer-humour/style.css new file mode 100644 index 00000000..343053d1 --- /dev/null +++ b/fetch/programmer-humour/style.css @@ -0,0 +1,25 @@ +body { + background-color: antiquewhite; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; +} + +h1 { + text-align: center; +} + +img { + display: block; + height: auto; + border: solid 2px black; +} + +#comic-alt { + margin-top: 16px; + font-style: italic; + color: black; + text-align: center; + font-size: 1.4rem; +}