The question is not about how <noscript> works, and why it's used, I'm fully aware of that.

Knowing that:

  • <noscript> only works when JavaScript is disabled by something browser-related (the browser itself or an extension). It doesn't work when JS is disabled by a firewall, for example.
  • A one-line alternative to <noscript> exists (using, well, JS) and covers every aspect of it + the above-mentioned firewall-ish case.

After a few Google searches, the number of people talking about alternatives to <noscript> and those trying to find reasons not to use <noscript>, makes me think there might be something wrong with <noscript>.

How come awesome websites such as Stack Overflow and Twitter are using <noscript> tags instead of the well-known alternative everyone seems to prefer?


The alternative is along those lines:

<script>document.write('<style>.noscript { display: none; }</style>');</script>
<div class="noscript">Y U NO JS ACTIVATED?</div>
有帮助吗?

解决方案

Stack Exchange is also warning one (using JavaScript) if it detects that jQuery has not been loaded from the googleapis.com CDN.

It seems to me that the combination of <noscript> and the jQuery detection covers most problems with corporate firewalls and browser settings/plugins. (And I even feel it is one better than just checking for JavaScript being blocked, as then still blocked downloads would go undetected.)

Sure, using the jQuery detection together with the proposed alternative would also cover situations where some software might somehow block JavaScript execution. But I wonder if the dynamically hidden text would then be indexed (and appear) in search engines.

But if that text is not a problem, then using the <div> might be a nice enhancement to detect if Stack Exchange's sstatic.net CDN is blocked. If that's blocked, the jQuery warning will not be given either. So with the <div> in the page source but the JavaScript that hides it in an external file downloaded from sstatic.net, one can easily detect any blocking. (Like Jukka explained in their answer it might then be better to not rely on styles either, but that's easily changed.)

(I fail to see how the alternative can detect JavaScript downloads being blocked in firewalls—which, in my limited understanding, block network traffic. But I assume that some security software running on workstations might indeed somehow make browsers stop executing JavaScript.)

其他提示

One moment there:

only works when JavaScript is disabled by something browser-related (the browser itself or an extension). It doesn't work when JS is disabled by a firewall, for example

So you want us to defensively code our site because you suspect there are firewalls out there that strip out SCRIPT tags and neglect to blank out the NOSCRIPT tags?

We are going to need a much more definitive, proven, argument about a real problem before hacking up our site and abandoning the use of the NOSCRIPT tag.

To answer the “why” question: because the noscript element is common legacy and often used routinely, typically just to show a message that tells the user to enable scripting. This can be useful enough in very special cases, like pages mostly used by developers and other people who actually switch scripting off and on.

Regarding your proposed alternative, it has the drawback of displaying “Y U NO JS ACTIVATED?” when scripting is enabled and stylesheets are disabled (or a special user stylesheet is in use, overriding the setting used). To replace noscript by JavaScript code, it is better to use code that actually removes the content of an element, or an element as a whole.

The thing of beauty in <noscript> is simplicty and Its pretty obvious.

The question is whether this

<script>document.write('<style>.noscript { display: none; }</style>');</script>
<div class="noscript">Y U NO JS ACTIVATED?</div>

Or this

<noscript>Y U NO JS ACTIVATED?</noscript>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top