Loading JavaScript with HTML script tags, execution timing, and error checks.

HTML Script Element: Load JavaScript at the Right Time

A script element in HTML connects JavaScript to a page, but its placement and attributes determine when the code runs. A script in an HTML document can execute before the element it needs exists. Load a small external file with defer to see the timing and result clearly.

<script> HTML example with an external file

Create two files in the same directory. Save the first as index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Script example</title>
  <script src="app.js" defer></script>
</head>
<body>
  <p id="status">Waiting for JavaScript</p>
</body>
</html>

Save this as app.js:

const status = document.querySelector("#status");
status.textContent = "JavaScript loaded";
console.log("Status updated");

Open the HTML file in a browser. The paragraph should display “JavaScript loaded,” and the browser console should show “Status updated.” The HTML script element points to app.js through src. The relative URL resolves against the document’s base URL; in this example the files are in the same folder.

The browser fetches the external classic script while it parses the page. Because defer is present, it executes the script after parsing finishes. The paragraph is then available to querySelector. The <script> HTML relationship is explicit: markup identifies the file, while the file contains the JavaScript that changes the page.

Inline and external code

You can put a short classic script directly in the document instead:

<p id="inline-status">Waiting</p>
<script>
  document.querySelector("#inline-status").textContent = "Ready";
</script>

A parser-inserted inline classic script runs when the parser reaches it. Here the paragraph is already in the document. If the same code appears in the head before the paragraph, the query can return null and the assignment will fail. Moving the script after the element or using a suitable loading strategy resolves that timing issue.

External files separate behavior from markup and can be reused across pages. Inline code can be convenient for a small example. When src supplies the script, put the executable code in that external file rather than between the opening and closing tags.

The type attribute controls how the content is treated. Omitting it loads a classic JavaScript script. type="module" selects a JavaScript module; modules have different scope and loading rules. A module without async is deferred by default. Use a local web server when trying modules, since opening files directly can encounter browser security restrictions.

Choose execution timing

For external classic scripts, defer preserves their document order and runs them after HTML parsing. This is useful when code needs page elements or depends on an earlier deferred script. By contrast, async fetches in parallel and runs as soon as it is available; relative execution order is not guaranteed. Use it for independent scripts that do not rely on the page being fully parsed.

Without either attribute, an external classic script encountered during parsing blocks the parser while it is fetched and executed. Inline classic scripts also execute at their position. These choices affect both correctness and how soon the browser can process the rest of the document.

If the paragraph still says “Waiting for JavaScript,” open developer tools. In Network, reload and check whether app.js was requested successfully; a failed request can indicate a wrong path or filename. In Console, inspect syntax errors or a null access. Correct the path or timing, reload, and confirm both the updated text and console message. If the request succeeds but the behavior is still wrong, inspect the response content and verify that the server returned the intended JavaScript file.