-
-
Notifications
You must be signed in to change notification settings - Fork 90
LONDON-JAN-25 | ANDREI FILIPPOV | Module-Data Flows | WEEK 3 feature/xkcd #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
b0740b4
3f74981
723dbfd
96c0445
6c15a28
8fb6833
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
const img = document.querySelector("img"); | ||
const errorMessage = document.querySelector("#errorMessage"); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const response = await fetch("https://xkcd.now.sh/?comic=latest"); | ||
const data = await response.json(); | ||
return data; | ||
} catch (error) { | ||
errorMessage.style.display = `contents`; | ||
errorMessage.textContent = error; | ||
} | ||
}; | ||
fetchData().then((data) => { | ||
img.setAttribute("src", data.img); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>humour</title> | ||
<link href="styles.css" rel="stylesheet" /> | ||
<script src="fetch.js" defer></script> | ||
</head> | ||
<body> | ||
<div id="errorMessage"></div> | ||
<img src="" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why assign an id attribute to one of the elements but not the other? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this because there’s only one image on the page, so I can select it by tag name. As for the div element, I wasn’t sure how many there might be in the future, so I decided to assign it an ID in advance. |
||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
img { | ||
margin: auto; | ||
margin-top: 30vh; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good practice to append a suffix like "Elem" to a name for variables that store a DOM element.