Homework
Homework — your first card
Homework — your first card, by hand.
- Create
js/components/card.jswithcreateCard(pic), as in the lesson. The card should have an image (alt+loading="lazy"), a title (<h2>) and a date. - Show it. In
main.jsimportcreateCard, build one card with fake data andappendChildit into#app. Serve it and check. - Prove the XSS difference. Set the
titleto'<b>test</b> <img src=x onerror="alert(1)">'. WithtextContentyou see the literal text (including<b>). Switch it toinnerHTML— the<b>renders andonerrortries to run. Switch back totextContent. Never leaveinnerHTMLfor dynamic text. - Extend the card. Add a short explanation (
pic.explanation) viatruncate(pic.explanation, 120), so long text doesn't blow up the layout.
Required tests:
- Structure test: the card has an
<article>with an image, title and date. - Security test: with
textContent, a "malicious" title is shown as text, not executed.
To submit: screenshots: (1) the card in the browser, (2) the same "malicious" title safely shown as text.
Next. The card exists, but it's dead — clicking does nothing. In lesson 3 we add events: the ♡ button, click-to-open, keyboard shortcuts — and meet the classic "bubbling" trap.