diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 00000000..ff262160 --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,18 @@ + + + + + Programmer Humour + + + + +

Latest XKCD Comic

+ +
+

Loading comic...

+
+ + + + \ No newline at end of file diff --git a/fetch/programmer-humour/script.js b/fetch/programmer-humour/script.js new file mode 100644 index 00000000..bea1168b --- /dev/null +++ b/fetch/programmer-humour/script.js @@ -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 = ` +

${data.title}

+ ${data.alt} +

${data.alt}

+ `; + }) + .catch(error => { + console.error(error); + + if (loading) { + loading.textContent = "Error loading comic 😢"; + } else { + container.innerHTML = `

Error loading comic 😢

`; + } + }); +} + +window.addEventListener("load", fetchComic); \ No newline at end of file diff --git a/fetch/programmer-humour/style.css b/fetch/programmer-humour/style.css new file mode 100644 index 00000000..6a031a65 --- /dev/null +++ b/fetch/programmer-humour/style.css @@ -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; +} \ No newline at end of file