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.htmlto open it in Chrome or Edge. If another browser opens it, right-click the file and choose Open with. The address bar starts withfileand ends inindex.html. - Opening
main.jsinstead 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
srcmatch 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:
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:
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:
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.