今天早上有个帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。

有人知道一些简单的方法来检测JavaScript是否被禁用吗?我的意图是给一个警告,如果浏览器没有启用JS,站点将无法正常运行。

最终,我想把它们重定向到能够在没有JS的情况下工作的内容,但我需要这个检测作为一个占位符来开始。


当前回答

Why don't you just put a hijacked onClick() event handler that will fire only when JS is enabled, and use this to append a parameter (js=true) to the clicked/selected URL (you could also detect a drop down list and change the value- of add a hidden form field). So now when the server sees this parameter (js=true) it knows that JS is enabled and then do your fancy logic server-side. The down side to this is that the first time a users comes to your site, bookmark, URL, search engine generated URL- you will need to detect that this is a new user so don't look for the NVP appended into the URL, and the server would have to wait for the next click to determine the user is JS enabled/disabled. Also, another downside is that the URL will end up on the browser URL and if this user then bookmarks this URL it will have the js=true NVP, even if the user does not have JS enabled, though on the next click the server would be wise to knowing whether the user still had JS enabled or not. Sigh.. this is fun...

其他回答

为了强制用户启用javascript,我将每个链接的“href”属性设置为相同的文档,这将通知用户启用javascript或下载Firefox(如果他们不知道如何启用javascript)。我将实际链接url存储到链接的“name”属性,并定义了一个全局onclick事件,读取“name”属性并将页面重定向到那里。

这对我的用户基础很有效,虽然有点法西斯;)。

这是我使用的“最干净”的解决方案:

<noscript>
    <style>
        body *{ /*hides all elements inside the body*/
            display: none;
        }
        h1{ /* even if this h1 is inside head tags it will be first hidden, so we have to display it again after all body elements are hidden*/
            display: block;
        }
    </style>
    <h1>JavaScript is not enabled, please check your browser settings.</h1>
</noscript>

noscript块在JavaScript被禁用时执行,通常用于显示你用JavaScript生成的替代内容,例如:

<script type="javascript">
    ... construction of ajaxy-link,  setting of "js-enabled" cookie flag, etc..
</script>
<noscript>
    <a href="next_page.php?nojs=1">Next Page</a>
</noscript>

没有js的用户会得到next_page链接——你可以在这里添加参数,这样你就可以在下一页知道他们是否通过js /非js链接来访问,或者试图通过js设置cookie,如果没有这些就意味着js被禁用了。这两个例子都非常琐碎,可以进行操作,但是您可以理解其中的含义。

如果你想知道有多少用户禁用了javascript,你可以这样做:

<noscript>
    <img src="no_js.gif" alt="Javascript not enabled" />
</noscript>

然后检查您的访问日志,看看这个映像被击中了多少次。这是一个略显粗糙的解决方案,但它将为你的用户基础提供一个很好的百分比。

上面的方法(图像跟踪)不适用于纯文本浏览器或根本不支持js的浏览器,所以如果你的用户群主要倾向于这方面,这可能不是最好的方法。

这是为我工作:它重定向访问者如果javascript被禁用

<noscript><meta http-equiv="refresh" content="0; url=whatyouwant.html" /></noscript>

我要在这里加上。02。虽然不是百分百防弹,但我觉得已经足够了。

对我来说,问题是,在我喜欢的例子中,提出一些“没有Javascript,这个网站不能很好地工作”的信息,然后你需要确保你的网站在没有Javascript的情况下也能正常工作。一旦你开始走上这条路,你就会开始意识到,关闭JS后,网站应该是防弹的,这是一大块额外的工作。

所以,你真正想要的是“重定向”到一个页面,上面写着“打开JS,傻瓜”。但是,当然,你不能可靠地做元重定向。所以,我的建议是:

<noscript>
    <style type="text/css">
        .pagecontainer {display:none;}
    </style>
    <div class="noscriptmsg">
    You don't have javascript enabled.  Good luck with that.
    </div>
</noscript>

...你站点中的所有内容都是用一个div类“pagcontainer”来包装的。noscript标签内的CSS将隐藏所有页面内容,而不是显示你想要显示的任何“无JS”消息。这实际上是Gmail所做的…如果它对谷歌来说足够好,它对我的小网站来说就足够好了。