Read a Page's HTML Like a Script

Priya’s page is served now, and its script loads as a module. But so far, the page contains a heading and a script line that you copied without reading closely. Soon, main.js will write the date into the page, list who’s coming, and show the games. Before a script can fill in part of a page, the page needs a place for the content, and the script needs a way to find that place.

In this lesson, you’ll read Priya’s HTML the way a script sees it, give the page an empty spot for each thing the script will fill, and check the result in the browser’s Elements panel. Start with the file you already have and the parts every page is made of.

Read the Page You Already Have

HTML looks like a jumble of angle brackets until you know its one repeating pattern. Here’s the heading line from Priya’s index.html:

Language: HTML
<h1>Priya's Game Night</h1>

This line is one element. It has a start tag, <h1>, then its content, then an end tag, </h1>, which repeats the name after a slash. The name says what the element is, so h1 marks the page’s main heading. The browser shows the content, and the tags tell it what that content is.

A start tag can also carry attributes, extra settings written as name="value". You’ve used two already, on the script line:

Language: HTML
<script type="module" src="main.js"></script>

Here, type and src are attributes of the script element. The element has no content, because its code lives in main.js, but it still needs its end tag.

Some elements can’t hold any content, so they have no end tag at all. These are called void elements. The meta line in your page’s head is one. <meta charset="utf-8"> stands alone.

The rest of the file is the outline every page has. The first line, <!DOCTYPE html>, tells the browser to read the file as current HTML. Everything else sits inside the html element, whose lang="en" tells the browser and screen readers that the page is in English. Inside html are two parts:

  • head holds information about the page that isn’t shown on it: the text encoding and the title that appears on the browser tab.
  • body holds everything that does show: the heading and, soon, the lists.

Your head is missing one line that every page should have. Open scratch/index.html in VS Code and add the viewport line under the charset line, so the head reads like this:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.