Full answer
The noscript element holds content that is used only when scripting is off or not supported. When scripts are on, the browser does not render it. It is allowed in the head (with link, style and meta) and in the body. Typical uses: a message such as "This page works better with JavaScript", a plain stylesheet link, or a fallback image for a script-driven tag.
Progressive enhancement is the idea behind it. Start with HTML that works for everyone, and add CSS and JavaScript as improvements for browsers that support them.
- HTML first: the content, links and forms work with no styles and no scripts. A form posts to the server, a link goes to a real address, and a menu is a list of links.
- CSS next: layout and appearance improve the page.
- JavaScript last: enhances the page, for example with validation messages, a menu button, or loading content without a page reload.
Reasons: scripts can fail to load because of a bad network, an error, a blocker or a slow device. Search engines and tools may read the HTML first. People with old browsers or assistive tools may not support a feature. Even users who have scripts on pass through a moment before they run.
Patterns:
- Use a
jsclass on thehtmlelement, set by a small script in the head, so CSS can hide things that need scripts only when scripts will run. - Do not hide content with a script-only reveal that can never appear if the script fails.
- Build links and buttons as real links and buttons that work first, and enhance them.
Scripts are on in this test, so the js class is added and the script-only paragraph is shown. The noscript content is not rendered, and a script sees it as plain text. The form works with no script at all, because it has an action and a method.
Test the page with scripts turned off to see what users get then.
<style>
.js-only { display: none; }
.js .js-only { display: block; }
</style>
<script>document.documentElement.classList.add("js");</script>
<p class="js-only" id="j">Shown only when scripts run.</p>
<noscript id="ns"><p>Please turn on JavaScript for the live preview.</p></noscript>
<form id="f" action="/subscribe" method="post"><label for="e">Email</label> <input id="e" name="email" type="email"> <button type="submit">Join</button></form>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- noscript content shows only when scripting is off.
- Start with working HTML, then CSS, then JavaScript.
- Forms and links should work without scripts.
- A js class on html lets CSS adapt.
- Test with scripts blocked.
Common mistakes
- Building a page that shows nothing without JavaScript when plain HTML would do.
- Using links with href="#" and click handlers instead of real addresses.
- Never testing with scripts blocked.
Follow-ups
- Where is noscript allowed?
- Why add a js class to the html element?
Tips
- Define the layers: HTML, CSS, JavaScript, and say why that order.
- Give the form example that works without scripts.