Sikau

JavaScript · Day 55 · about 35 min

Project: Daily quote card

A page that loads fresh content.

A page with fresh content

Let's combine DOM skills and fetch: a card that shows an inspiring quote, with a button for a new one. While it loads, it says "Loading…"; if something fails, it says so kindly.

Split drawing from loading

Keep two functions: showQuote(q) only draws a quote object on the page, and loadQuote() fetches one and passes it to showQuote. Drawing is then easy to test without the internet.

function showQuote(q) {
  document.querySelector("#text").textContent = "“" + q.quote + "”";
  document.querySelector("#author").textContent = "— " + q.author;
}

showQuote({ quote: "Little by little, a little becomes a lot.", author: "Tanzanian proverb" });

The rest of this lesson

  • Three states of a loading pageexplain
  • Testing without the internetexplain
  • Loading statesmatch
  • Twice as fast clickspredict
  • Build the quote cardchallenge
  • Badge quizquiz
  • Unit 9 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript