Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Scripting

The <script> element

Reference for the HTML <script> element: classic and module scripts, import maps, async and defer, integrity, crossorigin, fetchpriority and execution order.

The <script> element embeds or loads executable code, almost always JavaScript. The code can be written between the tags (an inline script) or loaded from a separate file with the src attribute (an external script). The type attribute decides what kind of script it is: a classic script, a JavaScript module, an import map, or a block of data that the browser does not run.

How and when a script runs matters a lot for page speed. By default, a classic script blocks the HTML parser: the browser stops building the page until the file has downloaded and run. The defer, async and type="module" options let the parser continue.

HTML
<!-- External module: downloads in parallel, runs after parsing -->
<script type="module" src="/js/app.js"></script>

<!-- Classic external script that waits for the parser -->
<script defer src="/js/menu.js"></script>

Try it

Execution order

The module script is written second but runs third, because module scripts are deferred: they wait until the parser has finished, then run just before DOMContentLoaded.

Attributes

Attribute Values Description
src URL Address of an external script file. When present, the element's content is ignored (it may only contain comments).
type Omitted, module, importmap, speculationrules, or another MIME type Kind of script. See The type attribute.
async Boolean For external classic scripts and all module scripts: fetch in parallel and run as soon as ready, without waiting for the parser or for other scripts.
defer Boolean For external classic scripts only: fetch in parallel and run, in document order, after parsing finishes and before DOMContentLoaded.
nomodule Boolean Don't run this classic script in browsers that support modules. Used for legacy fallbacks.
crossorigin anonymous, use-credentials Fetch the script with CORS. anonymous (also the value of an empty attribute) sends credentials only to the same origin; use-credentials always sends them.
integrity Hash such as sha384-โ€ฆ Subresource Integrity: the browser refuses to run the file if its content doesn't match the hash.
referrerpolicy no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url How much referrer information to send when fetching the script.
fetchpriority high, low, auto A hint about how urgently to fetch the script compared with other resources. Defaults to auto.
blocking render Holds back the first render of the page until this script has loaded and run. Use sparingly.
nonce Text A one-time token that lets this script run under a Content Security Policy that lists the same nonce.

It also accepts the global attributes. The language and charset attributes are obsolete, and type="text/javascript" is unnecessary: a script without type is already JavaScript.

The type attribute

  • Omitted, empty, or a JavaScript MIME type (such as text/javascript): a classic script. Variables declared at the top level become globals shared with other classic scripts.
  • module: a JavaScript module. It can use import and export, runs in strict mode, has its own top-level scope, is deferred by default, runs only once even if included twice, and is always fetched with CORS when it comes from another origin.
  • importmap: a JSON import map that tells the browser where bare module names such as "chart-lib" point. It must appear before any module script that relies on it, and it can't use src.
  • speculationrules: JSON rules that let the browser prefetch or prerender pages the user is likely to visit next. Not every browser supports it; others ignore the element.
  • Any other type (for example application/json or application/ld+json): a data block. The browser does not run it, but scripts can read its text. This is how structured data is usually added.
HTML
<script type="importmap">
  {
    "imports": {
      "chart-lib": "/vendor/chart-lib/index.js",
      "utils/": "/js/utils/"
    }
  }
</script>

<script type="module">
  import { drawPie } from "chart-lib";
  import { formatPrice } from "utils/money.js";
</script>

Execution order

Script Fetch When it runs Order between scripts
Classic, no attributes Blocks the parser Immediately, as soon as it is fetched Document order
Classic, defer In parallel After parsing, before DOMContentLoaded Document order
Classic, async In parallel As soon as it is fetched (may pause the parser briefly) Whichever finishes first
Module In parallel (with its imports) After parsing, before DOMContentLoaded Document order, together with deferred scripts
Module, async In parallel (with its imports) As soon as it and its imports are fetched Whichever finishes first
Inserted with JavaScript In parallel As soon as it is fetched Behaves like async, unless you set script.async = false

Three details are easy to miss:

  • defer and async have no effect on inline classic scripts; they always run immediately. Inline module scripts are deferred, and async does work on them.
  • If both async and defer are set on a classic script, async wins.
  • Scripts that read or change page elements must run after those elements exist. Use defer, type="module", or place the script at the end of the <body>.

Usage notes

  • Default to defer or modules. For most scripts, put <script defer src="โ€ฆ"> or <script type="module" src="โ€ฆ"> in the <head>. The file starts downloading early, and the page is never blocked waiting for it.
  • Use async for independent scripts. Analytics or ads that don't depend on other scripts and that nothing depends on are good candidates.
  • Third-party files. Add integrity and crossorigin="anonymous" to scripts from a CDN so a tampered file is rejected. Without crossorigin, errors in cross-origin classic scripts show only as "Script error." in window.onerror.
  • </script> inside inline code. The parser ends the element at the first </script, even inside a JavaScript string. Write it as <\/script> in your code.
  • Content Security Policy. Strict policies block inline scripts and on* attributes. Move code into files or use a nonce generated fresh for each response.
  • nomodule fallbacks are rarely needed now. Every current browser supports modules. Keep nomodule only if you still ship a separate bundle for very old browsers.
  • Keep <noscript> in mind. Content inside it appears only when scripting is off; build the page so the important parts work without JavaScript where possible.

Technical summary

Content categories Metadata content, flow content, phrasing content, script-supporting element
Permitted content Without src: depends on type, and must not contain the text </script. With src: nothing, or only comments and whitespace
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts metadata content, phrasing content or script-supporting elements
Implicit ARIA role No corresponding role
DOM interface HTMLScriptElement
Default display none

Browser support

Classic scripts, async, defer, modules, integrity and crossorigin work in every current browser. Import maps and fetchpriority are supported in all major browsers (Baseline since 2023 and 2024). blocking="render" and type="speculationrules" are not yet available everywhere; browsers that don't support them ignore them.