如何检测JavaScript是否被禁用? [英] How to detect if JavaScript is disabled?

查看:232
本文介绍了如何检测JavaScript是否被禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上有一篇文章询问有多少人禁用JavaScript。然后我开始想知道可以使用哪些技术来确定用户是否禁用了它。

有谁知道一些简短的方法来检测JavaScript是否被禁用?我的意图是给出一个警告,说明在没有启用JS的浏览器的情况下,网站无法正常运行。



最后,我想将它们重定向到能够在没有JS的情况下工作的内容,但我需要将此检测作为占位符来启动。

解决方案

我假设您正在尝试决定是否提供JavaScript增强内容。最好的实现会降级干净,以便该网站仍能在没有JavaScript的情况下运行。我还假定你的意思是服务器端检测,而不是出于不明原因使用< noscript> 元素。



执行服务器端JavaScript检测并不是一种好方法。作为替代,可以使用JavaScript设置cookie,然后在随后的页面查看时使用服务器端脚本测试该cookie。然而,这不适合决定要传送什么内容,因为如果没有Cookie,访客或阻止Cookie的访客将无法区分访客。

There was a post this morning asking about how many people disable JavaScript. Then I began to wonder what techniques might be used to determine if the user has it disabled.

Does anyone know of some short/simple ways to detect if JavaScript is disabled? My intention is to give a warning that the site is not able to function properly without the browser having JS enabled.

Eventually I would want to redirect them to content that is able to work in the absence of JS, but I need this detection as a placeholder to start.

解决方案

I assume that you're trying to decide whether or not to deliver JavaScript-enhanced content. The best implementations degrade cleanly, so that the site still operates without JavaScript. I also assume that you mean server-side detection, rather than using the <noscript> element for an unexplained reason.

There is not a good way to perform server-side JavaScript detection. As an alternative it is possible to set a cookie using JavaScript, and then test for that cookie using server-side scripting upon subsequent page views. However this would not be suitable for deciding what content to deliver as it would not be able to distinguish visitors without the cookie from new visitors or visitors who are blocking cookies.

这篇关于如何检测JavaScript是否被禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆