HTML <script> Tag

Beginner
7 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <script> element embeds executable code or data — most often JavaScript, either inline or via src. It can also hold JSON or other data when type is a non-JavaScript MIME type.

Control loading with async, defer, and type="module". Provide a no-JS fallback with <noscript>.

Baseline · Widely available.

Not deprecated. Core script support has been stable since about July 2015 (and earlier). Some attributes have uneven support.

Deprecated attributes.

Do not use charset or language on <script>. Documents are UTF-8; use type instead of language.

Experimental extras.

attributionsrc (Attribution Reporting) is newer and limited — skip it until you need that API.

HTML Syntax

syntax.html
<script src=" file.js"></script>

Both start and end tags are required (even when empty with src). Inline form: <script>/* JS here */</script>.

Attributes

The attributes you will use most often on <script>:

AttributeDescriptionTry it
srcURL of an external script (preferred for most pages)Try it
deferWith src: run after HTML is parsed, in document orderTry it
asyncWith src: fetch in parallel; run when ready (order not guaranteed)See edge cases
typemodule, importmap, or a data MIME type (omit for classic JS)Try it
nomoduleSkip in module-capable browsers — use for classic fallbacksTry it
integrity / crossoriginSubresource Integrity and CORS for trusted CDNsAdvanced
referrerpolicy / nonceReferrer for fetches; CSP nonce for allowed inline/external scriptsSecurity
fetchpriority / blockingFetch hint (high/low/auto); optional blocking="render"Newer

Global Attributes

The <script> tag also supports the Global Attributes in HTML.

Live Preview

Run a safe demo that updates the page (same idea as a tiny inline <script>), and compare common loading patterns.

pattern

Demo only — the button runs this page’s safe script, not remote files.

Preview

Click Run demo to update this text.

Inline classic script runs immediately when encountered.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline Classic Script

Put a small script in the page. Prefer external files for anything beyond tiny demos.

HTML
<p id="msg">Hello</p>
<script>
  document.getElementById("msg").textContent =
    "Hello from an inline script!";
</script>
Try It Yourself

How It Works

  1. Add an element the script can find (here, id="msg").
  2. Open <script> after that element (or wait for DOM readiness).
  3. Write JavaScript between the tags.
  4. Close with </script>. Classic inline scripts run as soon as the browser reaches them.

Example 2 — External Script with defer

Load a file without blocking HTML parsing; run it in order after the document is parsed.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>defer demo</title>
  <script src="app.js" defer></script>
</head>
<body>
  <p id="status">HTML is parsing…</p>
</body>
</html>
Try It Yourself

How It Works

  1. Point src at your .js file.
  2. Add defer so the browser finishes parsing HTML first.
  3. Keep document order when several deferred scripts depend on each other.
  4. Do not put defer on classic inline scripts — it has no effect without src (MDN).

Example 3 — type="module" + nomodule

Serve modern modules to capable browsers and a classic fallback to others.

HTML
<script type="module">
  document.getElementById("mode").textContent = "ES module";
</script>
<script nomodule>
  document.getElementById("mode").textContent = "Classic fallback";
</script>
<p>Mode: <span id="mode">…</span></p>
Try It Yourself

How It Works

  1. Set type="module" for ES module scripts (deferred by default).
  2. Add a second script with nomodule for browsers that do not support modules.
  3. Module-capable browsers ignore nomodule scripts.
  4. Omit defer on modules — it has no effect; modules already defer (MDN).

Example 4 — JSON Data Block

Embed data (not executable JS) with a non-JavaScript type, then read it from a normal script.

HTML
<script id="data" type="application/json">
  { "name": "Ada", "role": "Engineer" }
</script>
<p id="out"></p>
<script>
  var raw = document.getElementById("data").textContent;
  var user = JSON.parse(raw);
  document.getElementById("out").textContent =
    user.name + " — " + user.role;
</script>
Try It Yourself

How It Works

  1. Set type to a valid non-JS MIME type (e.g. application/json).
  2. Put JSON in the element body. The browser will not execute it as script.
  3. Give it an id so another script can find it.
  4. Parse with JSON.parse in a classic or module script.

Edge Cases & Pitfalls

Most bugs come from blocking the parser, misusing async/defer, or trusting untrusted script sources.

loading

async / defer need src

On classic inline scripts they have no effect (MDN). Prefer defer when order and DOM readiness matter; async when scripts are independent.

order

async does not keep order

If script B needs A, do not mark both async. Use defer (or modules) so they run in document order.

security

Never inject untrusted script text

Treat user content as data. Use CSP, and consider integrity for CDN scripts.

deprecated

Skip charset and language

UTF-8 is required for HTML documents. Identify language with type when needed.

a11y

Provide a no-JS path when needed

Use <noscript> for essential content or instructions when scripting is off.

Browser Support

The <script> element is core HTML in every browser. Module scripts, import maps, and some newer attributes have later start versions.

✓ Baseline · Widely available

<script>

Classic scripts are universally supported. ES modules are widely available in modern browsers; pair nomodule for legacy fallbacks when you must support older clients.

98% Classic scripts (approx.)
Google Chrome All / modules modern
Full support
Mozilla Firefox All / modules modern
Full support
Apple Safari All / modules modern
Full support
Microsoft Edge All / modules modern
Full support
Opera All / modules modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Classic only
Partial
<script> Widely supported

Bottom line: Safe for classic and modern module scripts. Prefer defer or modules to avoid blocking; skip deprecated charset/language.

Key Takeaways

  • Role: <script> embeds or loads JavaScript (or non-JS data blocks via type).
  • Loading: prefer src + defer (or type="module").
  • Fallback: nomodule for classic scripts when shipping modules.
  • Legacy: skip deprecated charset / language.

One line: load JS with <script src> + defer (or modules); keep inline scripts small.

Default CSS Settings

Scripts are not shown as page content. Typical user-agent styling:

default.css
script {
  display: none;
}

Do not rely on styling script for UI. Put visible messages in HTML elements your script updates.

Frequently Asked Questions

The script element embeds or loads executable code — usually JavaScript — or holds non-JS data blocks (for example JSON) when type is a non-JavaScript MIME type.
Both download without blocking HTML parsing when used with src. async runs as soon as the file is ready (order not guaranteed). defer runs in document order after parsing, before DOMContentLoaded. Neither applies to classic inline scripts.
External scripts often use defer in the head, or sit just before </body>. Modules defer by default. Avoid classic parser-blocking scripts in the middle of the page unless you need them immediately.
It treats the script as an ES module: deferred by default, can use import/export, and cross-origin loads use CORS. Pair with nomodule for a classic fallback on older browsers.
No for new work. Both are deprecated. Documents use UTF-8; omit charset. Use type instead of the old language attribute.
No. script is Baseline and widely available. Some attributes (charset, language) are deprecated; some newer ones (for example attributionsrc) are experimental.

Next: section

Group thematic content with the HTML section element.

section tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful