Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]-->
like image 858
Sean Avatar asked Aug 14 '12 08:08

Sean


People also ask

How many times we can apply if condition?

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.

When we have multiple conditions and out of which only one is true which type of conditional statement we use?

Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Can we put condition in else?

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.


2 Answers

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.

like image 175
DisgruntledGoat Avatar answered Sep 23 '22 23:09

DisgruntledGoat


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.

like image 31
Bruno Avatar answered Sep 24 '22 23:09

Bruno