Prior to its most recent beta, Microsoft Internet Explorer (IE), for better or for worse, differed significantly from version to version and from other popular browsers, requiring web developers to code some portions of their sites or web applications twice or more.
There are several browsers that are generally considered to be web-standards compliant. These are Mozilla Firefox, Google Chrome, Opera, Apple Safari, and the recently released IE9 beta, which renders pages more like Firefox or Chrome than previous versions of IE. In opposition to these compliant browsers are the various and sundry versions of IE from before the release of IE9.
Because of these differences — and here I mean even the differences between the various versions of IE — it can be very helpful to identify both that a user has IE and which version of IE is in use.
For example, I recently found a significant difference between how IE8, IE7, and IE6 calculated the offsetTop and offsetLeft properties of an element to which I wanted to dynamically add a modal window. The differences were enough that I was going to need to use slightly different calculations for each of these three versions of IE.
So I had to come up with a simple way to identify IE in JavaScript, and, frankly, I wasn't exactly sure what was the best or most efficient method to do it.
The Conditional Hack
After browsing some blogs, forums, and pages for suggestions, I found a hack that used IE's unique conditional comments to set a variable. Ultimately, I didn't use this technique, but I thought it was clever enough that I should mention it.
<!—[if IE 7]> <script> Ie = 7; </script> <![endif-->
Inserted in the head of an HTML document, conditional comments are only read by IE on Windows, so I could check for the variable in my external JavaScript files
If(ie == 7){
doSomething();
}
But using this technique required a lot of seemingly extra HTML.
<!—[if IE 6]> <script> Ie = 6; </script> <![endif--> <!—[if IE 7]> <script> Ie = 7; </script> <![endif--> <!—[if IE 8]> <script> Ie = 8; </script> <