针对特定浏览器从网站内禁用 JS
我的网站完全设计为没有 JavaScript。 JS是完全非侵入式添加的。
现在我不想在所有浏览器(尤其是较旧的 IE 版本)上测试 JS,因为它们不提供调试反馈,而且不值得我花时间。
所以我只想在某些浏览器上禁用 JS(或者相反:在我测试过的浏览器上允许它)。
有办法做到这一点吗?或者我是否必须创建一个 JS 标志,每次执行 JS 之前都必须查阅该标志? (真的不想这么做)
My Website is completely designed to without javascript. JS is added completely non intrusively.
Now I don't want to test the JS on all browsers (notably older IE versions), because they give no feedback for debugging, and it's just not worth my time.
So I'd simply like to disable JS on some browsers (or the opposite: allow it on the browsers I have tested it with).
Is there a way to do that? Or do I have to create a JS flag that I have to consult every time before I execute JS? (really don't want to do that)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这会起作用:将其作为第一个
标签
I believe this would work: have this as the first
<script>
tag在我看来,Cwallenpoole 的解决方案不会起作用,除非在 DOM 就绪事件上调用它,或者至少在其他脚本标记之后调用它,此时脚本已经运行了。
如果所有脚本都在一个地方,或者至少没有嵌入整个页面,也许您应该在检查浏览器后将它们包含在 JavaScript 中:
这是一个场景,说明了为什么要保留代码 DRY 并将所有内容集中在一处非常重要。
要检测浏览器,请查看 quirksmode,但请注意其对象检测页面的链接,如果这对您的用例更有用。 Yepnope.js 对于后者很有用,它是 Modernizr 的一部分。
Cwallenpoole's solution looks to me like it wouldn't work unless it's called on the DOM ready event or at least after the other script tags, at which point scripts would have already run.
If all your scripts are in one place, or at least not embedded all over the page, perhaps you should include them with JavaScript after checking the browser:
This is one scenario which illustrates why keeping your code D.R.Y. and all in one place is important.
For detecting browsers, check out quirksmode, but note the link to their Object detection page instead if that's more useful for your use case. Yepnope.js is useful for the latter, which is part of Modernizr.