9

I noticed Facebook does this where they have a meta refresh enclosed in a <noscript> enclosed in the <head> tag. They use this to detect if the user agent has javascript enabled or not.

We were thinking of using this method for 2 reasons:

  1. To auto redirect to a non-js optimized version of the site as facebook does.
  2. To include a non-js stylesheet to hopefully disable the finger pointer over javascript only anchors as well as for CSS3 complaint browsers, fading those buttons out to make it more obvious they are disabled (not working rather).

Will using this method for the above reasons cause any adverse effects that will affect a large percentage of user agents?

4

2 回答 2

7

<noscript>不,在头部使用标签不应该有任何过度的不利影响。

也就是说,有一些缺点<noscript>

  • 它仅在浏览器不支持 JavaScript 或被禁用时才有效。<noscript>如果 Javascript 被防火墙阻止,则标签的内容将不可见。
  • Javascript 支持很差的用户代理仍然会忽略<noscript>内容。
  • 虽然大多数浏览器都支持<noscript>头部,但它在技术上是无效的 X(HTML),因此您的页面可能无法验证。

IMO,您想要做的是一件非常好的事情,并且是对标签的适当使用。但是,更好的解决方案是在 Javascript 中使用功能检测来启用 Javascript 功能,而不是使用<noscript>来禁用它们。

如果您依赖 HTML5 功能,我会为此目的推荐Modernizr库。

于 2011-08-13T19:45:40.167 回答
3

对于第二个,我建议使用最初给你的<html>元素一个类的方法no-js,然后立即用 JavaScript 删除该类,并用一个类替换它js(或包括像Modernizr这样自动执行此操作的库)。这样,您可以将 CSS 样式定位到启用和禁用的 JavaScript 情况,方法是在这些样式前面加上.js.no-js

像这样的东西,例如:

.js span.javascriptOnlyAnchor { cursor: pointer; text-decoration: underline; }

.no-js span.javascriptOnlyAnchor { display: none; }

保罗爱尔兰在这里谈论更多:http: //paulirish.com/2009/avoiding-the-fouc-v3/

于 2011-08-13T19:43:45.560 回答