Disabling JS from within Website for specific browsers

旧时模样 提交于 2019-12-24 09:58:58

问题


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)


回答1:


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)
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/6669292/disabling-js-from-within-website-for-specific-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!