The <script> & <noscript> elements
The <script> tag adds JavaScript to a page, either written between the tags or loaded from a file with src. Add defer to load a file without blocking the page and run it once the HTML is ready, or type="module" for modern modules. The <noscript> tag holds content shown only when JavaScript is turned off.
Last updated: October 3, 2026
Quick facts
| Category | Metadata, flow, phrasing and script-supporting content (both) |
| Content model | <script>: JavaScript (or data for other types), or nothing when src is used. <noscript>: transparent fallback content |
| Tag omission | Neither the start nor the end tag can be omitted, for either element — even with src, <script> needs </script> |
| Permitted parents | Anywhere metadata or phrasing content is allowed: the <head> or the <body> |
| DOM interfaces | HTMLScriptElement, HTMLElement (<noscript>) |
Syntax
Live example
Edit the code below and watch the preview update instantly.
Attributes
<script>
| Attribute | Description |
|---|---|
| src | The URL of an external JavaScript file |
| defer | Downloads in the background and runs after the HTML is parsed, in order |
| async | Downloads in the background and runs as soon as it arrives, in any order |
| type | module for JavaScript modules, importmap, or a data type such as application/ld+json for structured data |
| nomodule | Runs only in old browsers that don’t support modules |
| crossorigin / integrity | Security options for scripts from another domain |
<noscript>
| Attribute | Description |
|---|---|
| — | Only the global attributes apply. |
Common examples
An external script that doesn’t slow the page
Load your JavaScript from a file with defer: it downloads in the background and runs once the HTML is ready.
A JavaScript module
Modules can import code from other files, and are deferred automatically.
Structured data for search engines
A script with the JSON-LD type doesn’t run at all — it carries structured data that Google reads to understand the page and show rich results.
Inline scripts vs external files
An inline script is written directly between <script> tags in the page; an external script lives in its own .js file loaded with src. External files are cached by the browser, so a script shared by many pages downloads only once, and they keep your HTML tidy and easier to maintain.
Inline scripts make sense for small amounts of code used on one page only, since they save an extra download. As a rule: shared or large code goes in a file, tiny page-specific snippets can stay inline.
| Inline | External file | |
|---|---|---|
| Cached between pages | No | Yes |
| Extra download | No | Yes, once |
| Best for | Tiny, page-specific code | Shared or large code |
Common mistakes to avoid
- Blocking scripts in the head. A plain
<script src>in the<head>stops the page from showing until it downloads and runs; adddefer. - Wrong file paths. A typo in
srcfails silently. Open your browser’s developer tools (Console tab) to see the 404 error. - Self-closing script tags.
<script src="app.js" />doesn’t work; always write the closing</script>. - Using
document.write. It’s slow and can wipe the page; change the page with DOM methods such astextContentorappendinstead.
Accessibility & SEO tips
- Put important content in the HTML itself rather than generating it only with JavaScript; search engines can run scripts, but content in the HTML is indexed faster and more reliably.
- Make sure anything your script makes clickable is a real <button> or link, so it works with the keyboard.
- Use
<noscript>to explain what visitors miss if JavaScript is turned off.
Browser support
Supported in every browser. The defer and async attributes and type=”module” work in all current browsers.
Tutorial: How to make a button