Encodera

Thanks for reading!

Donate to the site — we work hard for you.

HTML & CSS reference

<script>

Adds or embeds JavaScript; `<noscript>` shows fallback content when scripts are off.

Syntax

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

Attributes

ParameterTypeDescription
srcURLOptional. The address of an external script file.
defer—Optional. Run the script after the whole page is parsed.
typemoduleOptional. `module` enables JavaScript modules.

Examples

An embedded script changes the text on the page:

<p id="greeting">Loading…</p>
 
<script>
  document.getElementById("greeting").textContent = "Hello from JavaScript!";
</script>

An external file and fallback text:

<script src="/js/app.js" defer></script>
<noscript>
  <p>Please turn on JavaScript to use the calculator.</p>
</noscript>
<p>The calculator appears here.</p>

Notes

A plain <script> stops parsing the page until it is loaded and run. That is why external scripts are added with defer (or type="module") in <head>, or placed at the very end of <body>. A tag with src must have nothing inside it.

See also

  • <head> — The service part of the page: encoding, tab title, linked styles and scripts.
  • <body> — The visible part of the page: everything the user sees.
  • <template>, <slot> — A markup blueprint that stays hidden until JavaScript copies it; `<slot>` is an insertion point in web components.
  • defer, async — Attributes of an external `<script>` that load it without stopping the page from being parsed; they differ in when the script runs.

Related recipes

In the tutorial

  • Document structure — The skeleton of an HTML page — doctype, html, head and body, the encoding, the tab title, viewport and your first index.html file.

Thanks for reading!

Donate to the site — we work hard for you.