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]-->
You can have as many else if statements as necessary. In the case of many else if statements, the switch statement might be preferred for readability. As an example of multiple else if statements, we can create a grading app that will output a letter grade based on a score out of 100.
Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.
Conditional Statements Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
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.
It would be preferable for you to use feature detection rather than conditional comments. For example according to Microsoft documentation found here Internet Explorer 10 will completely ignore any conditional comments in your script such as:
<!--[if IE]>
This content is ignored in IE10.
<![endif]-->
You can patch this by adding:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
But this is not a long-term solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With