Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

script — embedding scripts and data

The script element embeds JavaScript or references an external file. The type attribute distinguishes classic scripts, modules, and data, while async and defer control execution timing.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<script>/* JavaScript */</script>
<script src="app.js" defer></script>
<script type="module" src="app.js"></script>

The script element embeds code or fetches it from src; inline code is not a fallback in that case. An end tag is required. An omitted or empty type means classic JavaScript, while type="module" means a JavaScript module.

For parser-inserted external classic scripts, omitting async and defer blocks parsing during fetching and execution. defer fetches in parallel and executes after parsing, in document order. async executes when ready without guaranteeing order; it takes precedence over defer.

Parser-inserted module scripts without the async attribute execute after the document has been parsed. Dynamically inserted module scripts can execute as soon as they are ready, even without the async attribute. defer has no effect on module scripts. For inline classic code, async and defer are not applicable. nomodule skips a classic script in browsers supporting modules.

type="importmap" defines import mappings, while type="speculationrules" defines speculative loading rules. type="application/json" creates a data block, not executable JavaScript. These three forms require inline content without src. Do not place untrusted content in executable scripts.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Script example</title>
</head>
<body>
  <p id="result">0</p>
  <script>
    document.getElementById("result").textContent = String(2 + 3);
  </script>
</body>
</html>

Result

5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources