在没有JavaScript的情况下隐藏需要JavaScript的元素
Hiding elements that require JavaScript without JavaScript

原始链接: https://0xda.de/blog/2025/04/hiding-elements-that-require-javascript-without-javascript/

本文讨论了如何处理网站上需要 JavaScript 才能正常工作的元素,确保在禁用 JavaScript 的用户也能获得优雅的回退体验。作者探讨了几种不同的技术,最终选择了一种简单有效的解决方案,利用 `

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 无需 JavaScript 即可隐藏需要 JavaScript 的元素 (0xda.de) 12 分,来自 mtlynch,1 小时前 | 隐藏 | 过去 | 收藏 | 1 评论 kaszus 5 分钟前 [–] 现在也有 CSS media 可以检测是否启用了 JavaScript。 大多数主流浏览器的最新两个版本都支持此功能。https://developer.mozilla.org/en-US/docs/Web/CSS/@media/scri... 回复 加入我们 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:
相关文章

原文

I’ve tried my best to make sure that this site works great (or at least reasonably well) even without JavaScript, but when JavaScript isn’t available, it can be a little clunky to hide things that do require it. With a mere 7 lines (or a one-liner if you’re nasty), you can easily hide elements that require JavaScript so that you don’t end up with broken functionality visible to users who have JavaScript off.

For context, I’m working on a small share button that I can add to my posts that makes it easy to share my posts however you’d prefer. So here’s an example of what that looks like right now without JavaScript:

Screenshot snippet showing a share SVG icon next to the permalink for the post

While it doesn’t look hideous, the link isn’t necessary when JavaScript is enabled, and the SVG isn’t necessary when JavaScript is disabled. I wanted to just show the SVG as a button you can press when JavaScript is enabled, and just show the link to make it easy to copy/paste when JavaScript is disabled.

If you’re not already familiar, the latter part is easy with the <noscript> HTML tag. This tag tells browsers to render its contents only when JavaScript is disabled. So making the link only show up when JavaScript is disabled is as easy as wrapping it in a <noscript> tag.

But if you want to make an element only appear when JavaScript is enabled, it’s a little less clear cut. There’s not an <onlyscript> tag, and the <script> tag has a very different meaning than the <noscript> tag.

Using JavaScript to indicate JavaScript is enabled

In my first approach with this, I thought maybe I could just add a line of JavaScript in the <head> tag that, if JavaScript is enabled, would run and add js-enabled to the class list on the <html> element.

This would work fine, then I could style things in a special way if JavaScript is enabled, by creating styles like this:

.share-button {
    display: none;
}
.js-enabled .share-button {
    display: block;
}

But this felt clunky. It results in needing to create two CSS rules for each element, and for my goal of just hiding things when JavaScript isn’t enabled, that seems heavy handed.

This might be a useful solution if you needed to do significantly more elaborate styling only when JavaScript is enabled, but for my purpose, it wasn’t simple enough.

Using noscript to override specific elements

The next solution I came to was to combine the use of the <noscript> tag with a <style> tag in the head of my site. Then I could just list out elements that I don’t want to show, and mark them explicitly as display:none;. They would still be present in the markup, but that’s fine, they shouldn’t get in anybody’s way.

In your site’s header, you can simply link all of your CSS as you normally would, and then add the following snippet to override the fields that you want to not show when JavaScript is disabled. In my example, I was hiding the .theme-toggle and the .menu-trigger classes. These are related to features that only work if JavaScript is enabled – the three way theme switcher and the hamburger menu dropdown on mobile.

<noscript>
    <style>
        .theme-toggle {
            display: none;
        }
        .menu-trigger {
            display:none;
        }
    </style>
</noscript>

This is alright. It’s explicit. It relies on existing browser behaviors to do selective style overrides when JavaScript isn’t available, so you don’t have to style both the JS-enabled and JS-disabled cases explicitly. But as I was developing new progressive enhancement features, I didn’t really like this pattern anymore. The more enhanced the website becomes, the longer this list of overrides becomes, and you have to make sure you always keep it up to date if you change class names.

⭐ Using noscript and a d-js-required class.

Since we can append any number of classes to an element, I realized that it makes much more sense to make this noscript-style as simple as possible. We’ll simply create a single class, d-js-required to indicate that JavaScript needs to be enabled for us to display this element. We can update our <noscript><style> block with just a single class, and then update our existing elements to also use this class.

<noscript>
    <style>
        .d-js-required {
            display: none;
        }
    </style>
</noscript>

So now we can have any number of simple elements that only get displayed when JavaScript is enabled, without writing another line of JavaScript, and without needing to maintain an ever-growing list of CSS overrides.

I hope this has given you some inspiration for creative solutions to solve progressive-enhancement problems. If you have JavaScript enabled, you can click the share button below to share this page! And if not, you can just copy the link down below to share this page!

联系我们 contact @ memedata.com