Disabling JS from within Website for specific brow

2019-08-15 02:57发布

问题:

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.