Lesson 49 of 51 · Module 7, lesson 9 of 11 · 3 min

Connecting JavaScript to your page

Connect a script to your page, and see how your HTML is already ready for JavaScript.

What you'll learn

  • How to connect a JavaScript file with <script>
  • What defer does, and where to put the script
  • How your HTML prepares the page for JavaScript

Why this matters

JavaScript makes pages react: a button that shows a message, a menu that opens, a form that checks answers as you type. You will learn it in the JavaScript course. This lesson shows only how to connect it, and why good HTML makes that work easier.

A script file

Create a folder js in my-website, and connect a file script.js in the <head>:

html
<script src="js/script.js" defer></script>
  • src is the path to the file — relative, as always.
  • <script> needs its closing tag, even when it is empty.
  • defer means: download the file now, but run it after the whole page has been read. Without it, a script in the head runs before the body exists, and cannot find your elements.

A small working example

In the playground, a script can also sit inside the page. Put it at the end of the <body>:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cooking tip</title>
</head>
<body>
  <h1>Cooking tip</h1>
  <p id="tip">Press the button to see today's tip.</p>
  <button type="button" id="show-tip">Show tip</button>

  <script>
    const button = document.getElementById("show-tip");
    const tip = document.getElementById("tip");
    button.addEventListener("click", function () {
      tip.textContent = "Taste the soup before you add more salt.";
    });
  </script>
</body>
</html>

What you'll see: a sentence and a Show tip button. Press the button: the sentence changes to the tip.

The page "Cooking tip" with the sentence "Press the button to see today's tip." and a "Show tip" button.

You do not need to understand the JavaScript yet. Notice only how it uses your HTML:

  • getElementById("show-tip") finds the element with that id (see The id attribute).
  • The button is a real <button type="button">, so it already works with a keyboard and does not submit anything (see Buttons).

Where the script goes

WhereUse
In the <head> with deferThe usual way for a script file
At the end of the <body>Also works: by then, the page above it exists

What happens when JavaScript fails

If a script has an error, or a visitor's browser blocks it, the rest of your HTML still works: headings, links and forms stay usable. That is another reason to build the page with correct HTML first, and add JavaScript on top.

To see errors, open the developer tools and choose the Console tab. Script errors appear there in red.

Try it yourself

  1. Select the example and choose Try it. Press Show tip.
  2. Change the tip text inside the script, and press the button again.
  3. Break it on purpose: change "show-tip" in the script to "show-tips". Nothing happens when you press the button. In your own browser, the Console shows an error. Fix it.

Common mistakes

  • A script in the head without defer. It runs too early and cannot find your elements. Fix: add defer, or put the script at the end of the body.
  • <script src="…" /> with no closing tag. The rest of the page may disappear. Fix: <script src="…"></script>.
  • A <div> as a button for JavaScript to click. Keyboard users cannot use it. Fix: <button type="button">.

Check yourself

  1. What does defer do?
  2. Where do you see JavaScript errors?
  3. Why should a page still work without JavaScript?

Answers

  1. It runs the script after the whole page has been read.
  2. In the developer tools, on the Console tab.
  3. Because scripts can fail or be blocked, and the content, links and forms should still work.

Next: before you publish anything, check that your HTML is clean and valid.

Chat with us on WhatsApp