Codectionary / Developer documentation / HTML

<script>

The <script> element embeds or references executable JavaScript code within an HTML document. It can contain inline code directly between its tags, or load an external file via the src attribute. Where scripts are placed and how they load (defer, async, or neither) significantly affects page load performance and execution order.

Syntax

<script src="app.js"></script>

Examples

External Script

Loading JavaScript from a separate file.

<script src="/js/app.js"></script>

Inline Script

Writing JavaScript directly within the HTML document.

<script>
  console.log("Page loaded");
  document.addEventListener("DOMContentLoaded", () => {
    console.log("DOM ready");
  });
</script>

Deferred and Module Scripts

Controlling load timing and using ES modules.

<script src="/js/analytics.js" defer></script>
<script type="module" src="/js/main.js"></script>

Attributes

  • src (URL): Path to an external JavaScript file
  • defer (boolean): Delays execution until after the document is parsed, preserving order
  • async (boolean): Downloads the script in parallel and executes as soon as it is ready, out of order
  • type (module | text/javascript): Specifies the scripting language or module type

Best practices

  • Place scripts before the closing </body> tag, or use defer, so they do not block HTML rendering
  • Use defer for scripts that need to run in order and depend on the full DOM being parsed
  • Use async only for independent scripts where execution order does not matter, like analytics
  • Prefer external scripts with src over large inline scripts for better caching and readability

Browser compatibility

Support applies to the named feature, not every attribute or behaviour in this article. Versions refer to browsers, not operating systems.

<script>

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1
Safari
Supported
From 3
Edge
Supported
From 12
Opera
Supported
By 12.1 (possibly earlier)

Mobile

Safari on iOS
Supported
From 2
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1 · Starting in Firefox 4, inserting <script> elements that have been created by calling document.createElement("script") no longer enforces execution in insertion order. This change lets Firefox properly abide by the specification. To make script-inserted external scripts execute in their insertion order, set .async=false on them.

Safari

  • From 3

Edge

  • From 12

Opera

  • By 12.1 (possibly earlier)

Safari on iOS

  • From 2

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
<script> src

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1
Safari
Supported
By 4 (possibly earlier)
Edge
Supported
From 12
Opera
Supported
From 15

Mobile

Safari on iOS
Supported
By 3.2 (possibly earlier)
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1

Safari

  • By 4 (possibly earlier)

Edge

  • From 12

Opera

  • From 15

Safari on iOS

  • By 3.2 (possibly earlier)

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
<script> defer

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 3.5
Safari
Supported
From 3
Edge
Supported
From 12
Opera
Supported
From 15

Mobile

Safari on iOS
Supported
From 2
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 1 · Chrome does not defer scripts with the defer attribute when the page is served as XHTML (application/xhtml+xml), see bug 41253514 and bug 41408348

Firefox

  • From 3.5 · Since Firefox 3.6, the defer attribute is ignored on scripts that don't have the src attribute. However, in Firefox 3.5 even inline scripts are deferred if the defer attribute is set.

Safari

  • From 3

Edge

  • From 12

Opera

  • From 15 · Opera does not defer scripts with the defer attribute when the page is served as XHTML (application/xhtml+xml), see bug 41253514 and bug 41408348

Safari on iOS

  • From 2

Chrome for Android

  • From 18 · Chrome Android does not defer scripts with the defer attribute when the page is served as XHTML (application/xhtml+xml), see bug 41253514 and bug 41408348

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0 · Samsung Browser does not defer scripts with the defer attribute when the page is served as XHTML (application/xhtml+xml), see bug 41253514 and bug 41408348
<script> async

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 3.6
Safari
Supported
By 4 (possibly earlier)
Edge
Supported
From 12
Opera
Supported
From 15

Mobile

Safari on iOS
Supported
By 3.2 (possibly earlier)
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 1

Firefox

  • From 3.6

Safari

  • By 4 (possibly earlier)

Edge

  • From 12

Opera

  • From 15

Safari on iOS

  • By 3.2 (possibly earlier)

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
<script> type

Desktop

Chrome
Supported
From 1
Firefox
Supported
From 1
Safari
Supported
By 4 (possibly earlier)
Edge
Supported
From 12
Opera
Supported
From 15

Mobile

Safari on iOS
Supported
By 3.2 (possibly earlier)
Chrome for Android
Supported
From 18
Firefox for Android
Supported
From 4
Samsung Internet
Supported
From 1.0

MDN BCD record for this feature · MDN reference

Support history and limitations

Chrome

  • From 1

Firefox

  • From 1

Safari

  • By 4 (possibly earlier)

Edge

  • From 12

Opera

  • From 15

Safari on iOS

  • By 3.2 (possibly earlier)

Chrome for Android

  • From 18

Firefox for Android

  • From 4

Samsung Internet

  • From 1.0
<script> type · module

Desktop

Chrome
Supported
From 61
Firefox
Supported
From 60
Safari
Supported
From 10.1
Edge
Supported
From 79
Opera
Supported
From 48

Mobile

Safari on iOS
Supported
From 10.3
Chrome for Android
Supported
From 61
Firefox for Android
Supported
From 60
Samsung Internet
Supported
From 8.0

MDN BCD record for this feature

Support history and limitations

Chrome

  • From 61 · Module scripts without the async attribute do not load when the page is served as XHTML (application/xhtml+xml). See bug 40518469.

Firefox

  • From 60

Safari

  • From 10.1 · Module scripts do not load when the page is served as XHTML (application/xhtml+xml).

Edge

  • From 79 · Module scripts without the async attribute do not load when the page is served as XHTML (application/xhtml+xml). See bug 40518469.
  • From 16 · Removed in 79 · Last supported version: 18

Opera

  • From 48 · Module scripts without the async attribute do not load when the page is served as XHTML (application/xhtml+xml). See bug 40518469.

Safari on iOS

  • From 10.3 · Module scripts do not load when the page is served as XHTML (application/xhtml+xml).

Chrome for Android

  • From 61 · Module scripts without the async attribute do not load when the page is served as XHTML (application/xhtml+xml). See bug 40518469.

Firefox for Android

  • From 60

Samsung Internet

  • From 8.0 · Module scripts without the async attribute do not load when the page is served as XHTML (application/xhtml+xml). See bug 40518469.

MDN BCD 8.1.5 · Dataset built 2026-10-08 · Mapping reviewed 2026-10-21. These dates are not individual browser-test or claim-verification dates. Unknown means the data does not establish support.

HTML Living Standard

Specifications & further reading