<script>
Adds or embeds JavaScript; `<noscript>` shows fallback content when scripts are off.
Syntax
<script src="app.js" defer></script>Attributes
| Parameter | Type | Description |
|---|---|---|
| src | URL | Optional. The address of an external script file. |
| defer | — | Optional. Run the script after the whole page is parsed. |
| type | module | Optional. `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.