针对特定浏览器从网站内禁用 JS

发布于 2024-11-23 18:00:51 字数 220 浏览 2 评论 0原文

我的网站完全设计为没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷途知返 2024-11-30 18:00:51

我相信这会起作用:将其作为第一个

if( /* test for browser */ ) killJS()
function killJS() {
    var scripts = document.getElementsByTagName("script")
    for( var i = 0; i < scripts.length; i++ )
    {
        var curr = scripts[i]
        if( curr.parentNode ) curr.parentNode.removeChild(curr)
    }
}

I believe this would work: have this as the first <script> tag

if( /* test for browser */ ) killJS()
function killJS() {
    var scripts = document.getElementsByTagName("script")
    for( var i = 0; i < scripts.length; i++ )
    {
        var curr = scripts[i]
        if( curr.parentNode ) curr.parentNode.removeChild(curr)
    }
}
君勿笑 2024-11-30 18:00:51

在我看来,Cwallenpoole 的解决方案不会起作用,除非在 DOM 就绪事件上调用它,或者至少在其他脚本标记之后调用它,此时脚本已经运行了。

如果所有脚本都在一个地方,或者至少没有嵌入整个页面,也许您应该在检查浏览器后将它们包含在 JavaScript 中:

<script>
    if( /* Browser checks */ )
        document.write( '<script src="scripts.js"></script>' );
</script>

这是一个场景,说明了为什么要保留代码 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:

<script>
    if( /* Browser checks */ )
        document.write( '<script src="scripts.js"></script>' );
</script>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文