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

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

问题描述

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

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.

有谁知道一些简短/简单的方法来检测 JavaScript 是否被禁用?我的目的是发出警告,指出如果浏览器未启用 JS,该站点将无法正常运行.

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.

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

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.

推荐答案

我假设您正在尝试决定是否提供 JavaScript 增强的内容.最好的实现会完全降级,因此该站点在没有 JavaScript 的情况下仍能运行.我还假设您的意思是 服务器端检测,而不是使用

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

没有很好的方法来执行服务器端 JavaScript 检测.作为替代方案,可以 使用 JavaScript 设置一个 cookie,然后在后续页面浏览时使用服务器端脚本测试该 cookie.然而,这不适合决定要提供什么内容,因为它无法区分没有 cookie 的访问者与新访问者或不接受 JavaScript 设置的 cookie 的访问者.

There is no 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 be unsuitable for deciding what content to deliver, as it would not distinguish visitors without the cookie from new visitors or from visitors who are did not accept the JavaScript set cookie.

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

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