How do you detect if JavaScript is disabled?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Not with JavaScript — if it is disabled, none of your code runs. The detection has to live in the HTML itself.
<noscript>
The browser renders the contents of this tag only when scripting is off:
<noscript>
<p class="warning">This site needs JavaScript to work properly.</p>
</noscript>
The CSS inversion trick
More useful in practice: assume JavaScript is off, then let a script prove otherwise.
<html class="no-js">
<head>
<script>document.documentElement.classList.replace("no-js", "js");</script>
</head>
.no-js .requires-js { display: none; }
.js .js-fallback { display: none; }
Because the inline script runs before the body paints, there is no flash of the wrong state. You can now style entire sections for each case rather than showing a single warning.
<noscript> can also carry CSS to hide interactive elements:
<noscript><style>.requires-js { display: none; }</style></noscript>
On the server, you cannot know
The request carries no signal about scripting. Anything server-rendered must assume nothing, which is why progressive enhancement is the real answer.
Build so the question matters less
Rather than detecting and apologising, make the core work without scripting:
- Use real
<form>elements withactionandmethodso submission works unaided, then enhance withfetch. - Use
<a href>for navigation, not click handlers on<div>. - Render content on the server; let JavaScript add interactivity on top.
This matters beyond the rare user who disabled scripting: it also covers a failed script load, a network blip, an ad blocker, and the first moments before hydration — situations far more common than JavaScript actually being switched off.
