You can accomplish this using different techniques, but implementing Conditional Comments is an easy way to detect IE versions 5-9. While using Conditional Comments may not offer a full solution for browser detection, it surely makes it easy to detect earlier versions of Windows Internet Explorer. For example, you can use Conditional Comments to have the browser load different Cascading Style Sheets (CSS) rules, or run different blocks of JavaScript code.

Basic Structure of the Conditional Comment

Conditional Comments use the same basic structure as an HTML comment . For this reason, all other browsers will interpret them as normal comments and will ignore the content within the comment. However, Internet Explorer versions 5-9 will recognize the special syntax within the comment and parse the content as if were normal web page content.

Example

Here is a list of example syntax used to target IE, specific versions of IE as well as non-IE.

Online Demo

Here is a listing of Conditional Comments. Take a look just after this listing to see which Conditional Comments were parsed by your browser. Try accessing this page with both IE and non-IE browsers to compare the results.

Results

Here are the results of the Conditional Comments listed above. If you are visiting this page with an IE browser pre-IE 10, you should have a few statements listed in the following container.

Conditional Comments for IE 10?

So here is the bad news… Support for Conditional Comments has been removed in Internet Explorer 10 for improved interoperability and compliance with HTML5. IE 10 browsers will treat Conditional Comments as regular comments, just like in other non-IE browsers. This change will most definitely impact pages written specifically targeting Windows Internet Explorer browsers. However, the good news is that you can continue to use other techniques that you may already be using for non-IE browser detection.