Loading video player…

Run Your First Page and Script

In this video, you gave Priya’s game night page its first two files and watched its script’s message arrive in the browser’s Console. The browser runs a script only when it loads a page whose script line names that file.

Key Points

  • In a terminal, you run a script file with a command. The browser works differently: it opens a page, and the page’s script line tells it which script to run.
  • <script src="main.js"></script> names a file next to the page, in the same folder.
  • For now, start every script with "use strict";, in double quotes, as its first line. It isn’t a message and never prints. It’s an instruction to the browser to be strict with this file.
  • Open the page, not the script. In File Explorer or Finder, double-click index.html to open it in Chrome or Edge. If another browser opens it, right-click the file and choose Open with. The address bar starts with file and ends in index.html.
  • Opening main.js instead gets you nothing useful: a browser only shows its code as text, and on Windows, a double-click can start another program that tries to run it and reports an error. Scripts open in the editor, pages in the browser.
  • The script’s output goes to that tab’s Console: Ctrl+Shift+J, or Cmd+Option+J on macOS.
  • The browser runs the script top to bottom, once, each time the page loads, and it reads the file from disk. After every change, save (the dot on the editor’s tab goes away), reload with Ctrl+R or Cmd+R, and read the Console. Your editor’s Auto Save can handle the save step.

When Your Message Doesn’t Show

If your message doesn’t show up, or an old one does, check the steps before the code:

  • Is the dot gone from the editor’s tab?
  • Did you reload the page?
  • Is this the Console of the page’s own tab, not the blank tab from the last lesson? Each tab has its own.
  • If a red line says a file wasn’t found, does the src match the file’s name in your editor’s side bar, letter for letter? The side bar shows the full name.

Set Up Your Course Folder

If you haven’t set up your folder yet, do it before you create the files. Install a current version of VS Code, the editor this course uses. In your file manager, create a folder called js-course, and inside it, a folder called scratch for practice files. Then, in VS Code, choose File > Open Folder and open js-course, not scratch. The Explorer panel shows js-course with scratch inside it, and the course keeps all its work there.

The Final Code

These are the finished files. Copy the page, index.html, into your scratch folder:

Language: HTML Filename: scratch/index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Game Night</title>
</head>
<body>
  <h1>Priya's Game Night</h1>
  <script src="main.js"></script>
</body>
</html>

The finished script, main.js. When you create it, write only its first two lines. The third is the line you add yourself before the reload:

Language: JavaScript Filename: scratch/main.js
"use strict";
console.log("Priya's game night page is loaded");
console.log(60 + 15 + 45 + 40);

After you save both files and reload the page, its Console shows:

Language: Program Output
Priya's game night page is loaded
160

In the next lesson, Serve Your Folder Locally, you’ll serve this folder from a small server on your own computer.

00:00 In the Console, you found that Priya’s games fit one evening without Catan. But reload the tab, and the sum is gone. To keep code, you put it in a file. In a terminal, you’d run a script file with a command.

00:14 The browser works differently. It opens a page, and the page tells it which script to run. In this video, you’ll give Priya’s page its first two files and watch its script’s message arrive in the Console.

00:28 In your editor, open your scratch folder for practice files, and create index.html in it. This file is the page.

00:38 Most of it is the outline every page has, with a heading for Priya’s Game Night. Copy the page from the text below this video. You’ll read HTML properly in a later lesson.

00:49 Look at the script line near the end. Its src names main.js, a file next to the page in the same folder.

00:59 Now create main.js beside it. Its first line is “use strict”, in double quotes. It isn’t a message. It’s an instruction to the browser: be strict with this file.

01:11 Nothing you write today needs it, so for now, start every script with this exact line.

01:17 The second line logs a message that says Priya’s game night page is loaded. Write only these two lines for now. Save both files.

01:28 Now open the page in Chrome or Edge. In File Explorer or Finder, double-click index.html. If another browser opens it, right-click the file and choose Open with.

01:41 The address bar starts with file, because the page came from your disk, and index.html sits at its end. Watch out: open main.js instead, and a browser only shows its code as text, or on Windows, another program tries to run it and reports an error.

01:58 Scripts open in the editor, pages in the browser.

02:03 The page shows its heading and nothing else. The script’s message goes to this tab’s Console: open it with Control Shift J, or Command Option J on a Mac.

02:14 The Console shows only the message. The strict line printed nothing, because it’s an instruction, not a message. Here’s the chain. The browser opened the page.

02:25 The page’s script line named main.js. The browser loaded that file and ran it, top to bottom, once.

02:35 Add a third line that logs the sum you worked out in the Console, the games without Catan.

02:41 The dot on the editor’s tab for main.js means the change isn’t saved yet. The browser only reads what’s on disk, so save, and the dot goes away.

02:53 The Console still shows one line, because the script ran when the page loaded, and the page hasn’t loaded since. Reload the tab with Control R, or Command R on a Mac.

03:04 The page loads again, and the script runs again from the top. The Console shows the message and then 160.

03:13 You’ll use this loop from now on. Edit, save, reload, and read the Console. Your editor’s Auto Save can handle the save step for you. When your message doesn’t show up, or an old one does, check the steps before the code.

03:31 Is the dot gone from the editor’s tab? Did you reload the page? Is this the Console of the page’s own tab, not the blank tab from the last lesson? Each tab has its own.

03:42 And if a red line says a file wasn’t found, does the src match the file’s name in your editor’s side bar, letter for letter? The side bar shows the full name.

03:53 Remember the rule. Open the page, not the script, and after every change, save, reload and read the Console.

04:02 Priya’s page and its script now run from your disk. In the next lesson, Serve Your Folder Locally, you’ll serve this folder from a small server on your own computer.

Become a Member to join the conversation.