Skip to main content

How to Add JavaScript to an HTML Page with the script Element

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The <script> element runs JavaScript in a web page. Write the code between <script> and </script>, or point its src attribute at a separate .js file. Put it at the end of body, so the elements it changes are already on the page.

Syntax​

Syntax
<script>
<code>
</script>

<script src="<path>"></script>
PartWhat it does
<script> … </script>Runs the JavaScript <code> written between the two tags.
src="<path>"Loads the JavaScript from a separate file instead, with a path relative to the HTML file.
</script>Closes the element. It's needed even when the element has src and nothing between its tags.

MDN's script page lists all its attributes.

The Example​

The example page has two paragraphs that say Waiting.... A script written inside the page changes the first one, and a script in a separate file, script.js, changes the second.

Step 1: Write the JavaScript File​

Create a folder for the example. In it, create a file named script.js:

script.js
document.getElementById('second').textContent = 'Set by script.js.';

It finds the element whose id is second and changes its text.

Step 2: Write the HTML Page​

In the same folder, create a file named index.html:

index.html, an HTML page with two paragraphs that say Waiting..., with the ids first and second, followed by a script element whose code sets the first paragraph's text to Set by the script in the page., and a script element with src script.js.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script element</title>
</head>
<body>
<p id="first">Waiting...</p>
<p id="second">Waiting...</p>

<script>
document.getElementById('first').textContent = 'Set by the script in the page.';
</script>
<script src="script.js"></script>
</body>
</html>

The first script element holds its code inside the page. The second loads script.js from the same folder. Both come after the paragraphs, at the end of body.

Step 3: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium with two lines: Set by the script in the page. and Set by script.js.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The browser ran each script when it reached it, and by then both paragraphs were already on the page.
  • The first paragraph's text came from the code inside the page, and the second's from script.js.
  • Neither says Waiting... any more, because the scripts changed them as the page loaded.

Common Mistakes​

MistakeWhat happensDo this instead
Writing <script src="script.js" /> with a slash and no closing tagThe rest of the page disappears: the browser read everything after it as part of the script, so the page showed only the paragraph above it, and script.js didn't runAlways close it: <script src="script.js"></script>
Writing code between the tags of a script element that has srcThat code never runs: in a test, only script.js ran, and the code between the tags was ignoredUse two script elements, one with src and one with the code
Putting the script in head without deferIt runs before the elements exist: the browser's console showed Cannot set properties of null (setting 'textContent')Put the script at the end of body, or add defer

Troubleshooting​

SymptomLikely causeFix
Everything after a script tag is missing from the pageThe script element isn't closedAdd </script>
Cannot set properties of nullThe script ran before the element it changes was on the pageMove the script to the end of body, or add defer
The code between the script tags doesn't runThe element also has srcMove the code into its own script element

FAQ​

Where should the script element go?​

At the end of body, as in the example, or in head with the defer attribute: <script src="script.js" defer></script> waits until the browser has read the whole page. In a test, a script in head with defer changed the paragraph just like the one at the end of body.

Do I need type="text/javascript"?​

No. JavaScript is the default, so a plain <script> runs JavaScript, as in the example.

Can one page have several script elements?​

Yes. The example has two, and the browser ran both.

Conclusion​

The script element runs JavaScript, either from the code between its tags or from the file named in src. Always close it with </script>, and put it after the elements it works with.

Additional Resources​