CSS Reference ยท Media Queries and Features
The scripting media feature
Reference for the CSS scripting media feature: detect whether JavaScript is available, its none, initial-only and enabled values, with examples and caveats.
The scripting media feature tells CSS whether scripts (JavaScript) can run in the current document. It has three states, because some outputs, such as a printed page, run scripts at load time but cannot respond to later interaction.
It is a way to style a page for the "no JavaScript" case from CSS alone, without adding a no-js class by hand.
@media (scripting: none) { .needs-js { display: none; } }
Try it
In a normal browser with scripting on, the label says enabled. In print preview or when scripts are disabled you would see a different message.
Syntax
@media (scripting: none) { /* ... */ } @media (scripting: initial-only) { /* ... */ } @media (scripting: enabled) { /* ... */ }
It is a discrete media feature.
Values
| Value | Description |
|---|---|
none |
Scripting is not available at all (disabled, or unsupported by the output). |
initial-only |
Scripts run during the initial page load but not afterward, as in some print and static outputs. |
enabled |
Scripting is available and the page can respond to events. |
Usage notes
- Progressive enhancement. Build pages that work without scripts, then use
(scripting: enabled)for styles that only make sense with scripts, such as a collapsed accordion that script opens. - Hide script-only controls. Under
(scripting: none)you can hide buttons that need JavaScript, or show a fallback instead. - It does not report errors. A script that fails to load still counts as
enabled. The feature reflects the environment, not whether your code worked. - The
<noscript>element.<noscript>content shows when scripting is off, but CSS-only changes are easier with this media feature. - Print. Printed output typically reports
initial-onlyornone, so interactive widgets can be simplified there. - Complement, not replacement. The server cannot see this feature; it is evaluated in the browser.
Browser support
Supported in current versions of the major browsers; check a compatibility table for older releases.
Related
print and screen typesโ media types@mediaโ the at-rule- Lesson: Media Queries