Why is Stylesheet loaded when Contional Comment states it should be ignored?

Why is Stylesheet loaded when Contional Comment states it should be ignored?

平定天下 发布于 2021-11-26 字数 956 浏览 736 回复 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!!!

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2022-06-07 1 楼

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.