Targeting specific versions of Internet Explorer and other browsers with conditional comments
Posted June 18th, 2008 in HTML and CSS
Internet Explorer's conditional comments can be useful for applying different style sheets to older versions of Internet Explorer without having to hack your style sheets up too much. There are several different ways to target versions of Internet Explorer with condition comments by making the code between the conditions look like a regular HTML comment to other browsers. It is also possible to target particular versions of IE and also let all other browsers access the code between the comments. This post looks at how to do this, using a Javascript include file as an example.
If you wanted to include a Javascript library, like jQuery for example, for only Internet Explorer 6.0 and higher, and also all other browsers, you could do something like this:
<!--[if gte IE 6]>
<script language="javascript" type="text/javascript" src="/js/jquery-1.2.3.js"></script> <![endif]--> <![if !IE]> <script language="javascript" type="text/javascript" src="/js/jquery-1.2.3.js"></script> <![endif]>
The first condition says that for Internet Explorer 6 and higher use the code between the tags and include the Javascript file; older versions will ignore the code and not include the file. The second condition says all non Internet Explorer browsers should use this code and include the file.
The only problem with the above example is you end up having to put the same <script> tag in twice and use up more space than is necessary. Fortunately it's possible to combine the two into a single conditional comment like so:
<!--[if gte IE 6]><!--> <script language="javascript" type="text/javascript" src="/js/jquery-1.2.3.js"></script> <!--<![endif]-->
The same code example again, but this time with emphasis so you can see the additional parts added to the first conditional comment in the first example:
<!--[if gte IE 6]><!--> <script language="javascript" type="text/javascript" src="/js/jquery-1.2.3.js"></script> <!--<![endif]-->
This works the same way as the first example above but is more concise and only requires the <script> tag to be in the HTML code once.
Related posts:
- Remove cellpadding and cellspacing from an HTML table with CSS (Wednesday, November 5th 2008)
- Style an HTML form input with CSS and jQuery (Saturday, July 26th 2008)
- Internet Explorer 8's compatibility flags (Friday, May 30th 2008)
- Using Internet Explorer Conditional Tags for Style Sheet Selection (Wednesday, May 21st 2008)
- Centering a page with HTML and CSS (Tuesday, May 6th 2008)
Recent posts:
- List installed packages with YUM (Tuesday, December 2nd 2008)
- Monthly Roundup - November 2008 (Monday, December 1st 2008)
- Weekly Roundup - December 1st 2008 (Monday, December 1st 2008)
- Installing subversion on CentOS (Sunday, November 30th 2008)
- GoDaddy 99 cent .com domain coupon code (Saturday, November 29th 2008)
- Find the index of a string within a string with Javascript (Friday, November 28th 2008)
Subscribe to RSS Feed / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email and have my posts sent in a daily email.
