views:

230

answers:

1

I thought conditional comments would instruct the browser to ignore the content if the condition is not met?!

For example I want to only include a stylesheet if IE6 is the browser. The following in located in the <HEAD> element of the page.

<!--[if IE 6]>
  <link id="IE6StyleSheet" rel="Stylesheet" type="text/css" href="~/css/IE6.css" runat="server" />
<![endif]-->

or

<!--[if IE 6]>
  <link rel="Stylesheet" type="text/css" href="../css/IE6.css" />
<![endif]-->

How come IE7, IE8 and FF3 all load that stylesheet?!

NOTE: Changing the condition to [if lte IE 6] does not make any difference! :(

MAJOR UPDATE

I am a moron... I just noticed what I did wrong! The example I'd given was slightly modified. The path to the css file in under App_Themes! Of course the css was always loaded!!!

+3  A: 

Try:

<!--[if lte IE 6]>
   <link id="IE6StyleSheet" rel="Stylesheet" type="text/css" href="../css/IE6.css" />
<![endif]-->

This will only load the stylesheet for IE6 or lower versions. Here's a test script you can use, it will print out which version of IE you're using:

<p><!--[if IE]>
According to the conditional comment this is Internet Explorer<br />
<![endif]-->
<!--[if IE 5]>
According to the conditional comment this is Internet Explorer 5<br />
<![endif]-->
<!--[if IE 5.0]>
According to the conditional comment this is Internet Explorer 5.0<br />
<![endif]-->
<!--[if IE 5.5]>
According to the conditional comment this is Internet Explorer 5.5<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is Internet Explorer 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is Internet Explorer 7<br />
<![endif]-->
<!--[if gte IE 5]>
According to the conditional comment this is Internet Explorer 5 and up<br />
<![endif]-->
<!--[if lt IE 6]>
According to the conditional comment this is Internet Explorer lower than 6<br />
<![endif]-->
<!--[if lte IE 5.5]>
According to the conditional comment this is Internet Explorer lower or equal to 5.5<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is Internet Explorer greater than 6<br />
<![endif]-->
</p>

You should not see any text in Firefox with this test code.

John Rasch
Unfortunately I had started with [if lte IE 6] and alas it also does not seem to work :(
BlackMael
Well what does the test script display in the browser?
John Rasch
http://www.positioniseverything.net/articles/sidepages/example2.htmlThis page seems to work across my various browsers.My question is why does my example above not work as expected
BlackMael
Oh and the example above correctly picks the browser version.Even so...<!--[if IE 6]> <link to stylesheet /><![endif]-->Still loads the stylesheet even if the browser is not IE6
BlackMael
Problem solved now.. see above.
BlackMael