Skip to main content

HTML Tags

HTML <script> Tag

Written by Updated HTML <script> Tag exercises

The script tag adds JavaScript to a page, either written straight inside it or loaded from a separate .js file. Where you put it, and whether you add defer, decides whether your page shows up fast or sits blank while the script loads.

Syntax

html

<script>
  // JavaScript code here
</script>

<!-- OR referencing an external file -->
<script src="script.js"></script>

The first form runs the code written inside. The second loads it from a file, which is what most sites do: the browser can cache the file and reuse it on every page. A script with src must be empty inside; any code between the tags is ignored.

Attributes

AttributeWhat it does
srcThe address of an external JavaScript file.
deferDownloads the file in the background and runs it after the HTML has been read, in order. The safe default for most scripts.
asyncDownloads in the background and runs as soon as it arrives, in any order. Good for independent scripts like analytics.
typeLeave it out for normal JavaScript. Use type="module" for ES modules.
crossoriginLoads the script with CORS, needed to get detailed error messages from scripts on another domain.
nomoduleRuns only in old browsers that do not understand modules. Rarely needed today.

Example

html

<!DOCTYPE html>
<html>
<head>
  <title>Script Tag Example</title>
  <script>
    function showMessage() {
      alert("Hello! This is a message from JavaScript.");
    }
  </script>
</head>
<body>
  <h1>Click the button to see the message</h1>
  <button onclick="showMessage()">Click Me</button>
</body>
</html>

Click the button and a message pops up. The function is defined in a script in the <head>, and the button calls it with onclick.

Why the position and defer matter

The browser reads HTML from top to bottom. When it meets a normal <script src>, it stops, downloads the file, runs it, and only then continues with the page. A big script in the <head> therefore means a blank screen.

It also causes the most common beginner error: a script in the <head> that looks for an element with document.getElementById() finds nothing, because the element further down has not been read yet.

Two fixes:

Recommended: in the head, with defer

html

<head>
  <script src="app.js" defer></script>
</head>

or put the <script> just before </body>, after all the elements it needs. defer is the cleaner choice: the file downloads early, and runs only once the page is ready.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Never put secrets like API keys in a script. Anyone can read it with View Source.
  • type="text/javascript" is the default and can be left out.
  • Modules (type="module") are deferred automatically. See JavaScript Modules.
  • For what JavaScript can do once it runs, start with How to Add JavaScript.

HTML <script> Tag exercises

Load a script

Write the line that runs this script file on a page: https://html5andcss3.org/samples/first.js

Result

Check your answer to see it here.