Search code examples
javascripthtmlcssconditional-comments

Multiple conditional statements in <head> of document


I've got a script I want to run in every browser except versions of IE below 9, obviously, this conditional statements works for IE:

<!--[if gt IE 8]>
    JavaScript stuff here
<![endif]-->

But that code is then not executed in any other browser except IE9.

Is there any way of using multiple conditional statements together, e.g.

<!--[if gt IE 8 OR !IE]>
    JavaScript stuff here
<![endif]-->

Solution

  • You use a conditional comment like this:

    <!--[if gte IE 9]><!-->
        <script></script>
    <!--<![endif]-->
    

    There are two types of conditional comment: ones where the entire block is commented out of all browsers (your first version above) and ones where only the "condition" is commented out (this version here).

    So all non-IE browsers see <script></script> outside of comments, IE before v9 parse the comments and know to ignore that HTML.