如何检测客户端是否已从 Rails 打开/关闭了 javascript?
我不想与关闭了 javascript 的用户打交道,所以我只想阻止他们使用我的应用程序。我想检测用户是否打开了 javascript,以便我可以将他们重定向到一个页面,告诉他们需要使用 javascript。我确定它在请求标头中。任何人有任何想法我可以如何做到这一点?
I don't want to deal with users who have javascript turned off, so I want to just prevent them from using my application. I want to detect if the user has javascript turned on so I can redirect them to a page that tells them they need to use javascript. I'm sure its in the request header. Anyone have any ideas how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
noscript
标签是最简单的解决方案,尽管不完全是您正在寻找的解决方案。The
noscript
tag is the easiest solution, though not exactly whay you're looking for.您真的需要重定向吗?将消息放入
元素中,它只会显示给它所适用的对象。
Do you really need a redirect at all? Put the message in a
<noscript>
element, and it will only be displayed to those it applies to.你不能。服务器不知道客户端启用了什么。你甚至无法“检测”客户端是否禁用了 Javascript(因为你会用什么来做到这一点?)。
您可以使用技巧在禁用 Javascript 时显示内容(例如,
标记,或使用 Javascript 隐藏仅适用于 JS 的内容)。
You can't. The server has no idea what the client has enabled. You can't even "detect" if Javascript is disabled from the client (because what would you use to do so?).
You can use tricks to show content when Javascript is disabled (for instance, a
<noscript>
tag, or using Javascript to hide content that is meant for JS-only).无法在服务器端检测 JavaScript。但是,您可以使用
NOSCRIPT
标记来显示一条消息,指示您的网站需要 JavaScript。例如,在浏览器中关闭 JavaScript 并加载 Stack Overflow 页面。页面顶部会出现一条令人讨厌的红色消息,警告您该网站在启用 JavaScript 的情况下工作效果最佳。您可以检查 Stack Overflow 源代码和 CSS 样式,了解它们是如何实现的。
如果您确实想阻止非 JavaScript 用户使用您的网站,您可以将消息放在
noscript
标记内的div
中,并展开div 覆盖页面上的所有内容。
There is no way to detect JavaScript on the server side. However, you can use a
NOSCRIPT
tag to display a message indicating that your website requires JavaScript.For an example, turn JavaScript off in your browser and load a Stack Overflow page. There'll be an obnoxious red message across the top of the page warning you that the site works best with JavaScript enabled. You can examine the Stack Overflow source code and CSS styling to see how they implemented it.
If you really want to deter non-JavaScript users from using your website, you could put your message in a
div
inside thenoscript
tag and expand thediv
to cover everything on the page.您只需要 html:
这当然不会阻止服务器提供内容,它只会输出此
meta refresh
标记,该标记将由获得的客户端解析(并通过重定向用户来执行)没有 JavaScript 或禁用 JavaScript。You only need html:
This of course won't prevent the server from serving content, it only will output this
meta refresh
tag, which will be parsed (and executed by redirecting the user) by clients that got no javascript or javascript disabled.您可以拥有一个包含一段 javascript 的登录页面,该片段运行并重定向到真正的 Railsy 登录页面,或者执行 AJAXY 调用来获取您想要向用户显示的数据。这肯定需要启用 JavaScript 才能继续。
You could have a landing page with a snippet of javascript that runs and redirects to the real railsy landing page, or perform an AJAXY call to get the data you want to show the user. This would positively require javascript to be enabled to continue.