Block script in IE

2019-06-20 09:18发布

问题:

I have a fancy script that is nice, but not essential and surprise surprise, doesn't play nice with IE. How do I 'comment it out' for IE?

I know I can use the following to include statements for IE, but how do I exclude them?

<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->

回答1:

Just use ! see here for more info

<!--[if !IE]>

or

<!--[if !(IE 6)]>


回答2:

For all IE versions:

<!--[if !IE]>
conditional stuff
<![endif]-->


回答3:

Unfortunately, there's no such mechanism as to exclude a script (i.e. unless the script is targeted at IE only, in which case go see ramblex/karim79's answer).

But ... you could modify your script to check for a global (yeah, I know sigh) variable that - when set - makes the script stop. Something along the line:

// wrap your nice script in an anonymous function
(function(document, undefined) {
    if ( window.ie6 === true ) return;

    .....
})(document);

Now go on and include the global variable with a conditional tag, like so:

<!--[if lte IE 6]>
    window.ie6 = true;
<![endif]-->

Et voila.