Scripts & Templates
HTML <script> element
Embeds or references executable scripts or supported data blocks. Execution depends on type and attributes such as async and defer. Classic and module scripts have different loading and execution rules. External classic scripts can block parsing unless async or defer changes scheduling. Modules are deferred by default. Use trusted resources, appropriate CSP and integrity metadata when applicable. It accepts global HTML attributes; individual global features may be inapplicable or restricted.
Formal Syntax & Structure
html syntaxhtml
<script src="app.js" defer></script>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| src | URL | Specifies the resource to load for the given element, such as an image, script or media file. The resource type, loading behavior and security rules depend on the element. |
| type | script type keyword or MIME type | Omit for classic JavaScript; module selects a JavaScript module. importmap and speculationrules have separate parsing and support rules. |
| nomodule | boolean | Prevents an external classic script from executing in browsers that support JavaScript modules. Used for legacy fallbacks; not needed for ordinary modern module scripts. |
| async | boolean | Allows eligible external classic scripts to execute as soon as they are ready, without preserving document order. Async module scripts may also execute when their dependency graph is ready. |
| defer | boolean | Schedules an external classic script to run after parsing and before DOMContentLoaded, in document order. It has no effect on inline classic scripts; modules defer by default unless async is used. |
| crossorigin | enumerated: anonymous, use-credentials | Configures CORS settings for eligible resource requests. anonymous omits cross-origin credentials; use-credentials includes them. Server CORS headers must permit the request when needed. |
| integrity | subresource integrity hash metadata | Pins eligible external resources to an expected cryptographic digest. The browser rejects mismatches. Cross-origin use requires appropriate CORS support; regenerate the digest when the resource changes. |
| referrerpolicy | referrer policy keyword | Controls the referrer information sent with an eligible fetch or navigation. no-referrer omits it; strict-origin-when-cross-origin is a common default policy. It does not prevent all tracking. |
| blocking | space-separated tokens | Requests eligible resource processing to block rendering; render is the relevant token. This can delay visible content, so use only when needed. |
| fetchpriority | enumerated: high, low, auto | Hints relative fetch priority for eligible resources. It is not a guarantee and does not replace preloading or lazy loading. Reserve high for genuinely important resources. |
| nonce | server-generated unpredictable token | Matches a CSP nonce source to authorize an eligible script or style. Generate a fresh unpredictable value per response; never reuse a fixed example nonce in production. |
Return Value
HTMLScriptElement
Content model: Text governed by the applicable style or script language; no ordinary HTML child elements.
Working Example
Example Snippethtml
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>HTML example</title> </head> <body> <p id="message">Waiting</p> <script>document.getElementById('message').textContent = 'Ready';</script> </body></html>Browser Compatibility
Minimum supported versions across modern rendering engines
Chrome
1
Yes
Edge
12
Yes
Firefox
1
Yes
Safari
3
Yes
Opera
≤12.1
Yes
Checked desktop BCD snapshot: script element, not modules or all script types. Versions denote the recorded start of support (≤ means at least that early), not a guarantee of all behavior. Unresolved mirror and preview rows omitted. Firefox changed dynamic insertion ordering in version 4.
HTML Living Standard