JavaScript to detect Internet Explorer and it's version number
Yep, another way to do it and hopefully compliments the methods already out there, which include html’s conditional classes, object detection, dynamically adding conditional comments and user agent sniffing (for the brave or naughty).
What I like about this script is it's just JavaScript, can be run immediately and anywhere in the HTML, and is small.
Download and use the demo or try dave-smith.info/GitHub/isIE/demo/
IE version and compatibility mode correctly identified in:
- 10 (Win8)
- 9 (Win7 SP1)
- 8 (WinXP SP3)
- 7 (Win Vista SP2)
- 6 (WinXP SP3)
If a browser or version isn't mentioned here then it hasn't been tested; more tests welcomed.
isIE creates two variables:
- isIE: The browser major version number
- isIEmode: The compatibility mode major version number
isIE is calculated using Conditional Compilation, which could roughly translate as IE conditional comments in JavaScript. The key of which is @_jscript_version, a variable providing a version number which can be used to identify IE versions, see Conditional Comments in JScript.
By default isIE is false, so all non-IE browsers get false. Only browsers that know Conditional Compilation comments set isIE to something else.
When IE is detected isIE is set to the major version number, for example IE 10 will set isIE = 10. The only exception to this is for all IE lte 5.5 which sets isIE = 5.
In testing I found that isIE gives the browser version even if the compatibility mode is changed, for example if IE 10 is used in IE 7 mode, then IE 10 will be the detected version. Hence isIEmode is also created as knowing both may be useful.
isIEmode is calcaulated using the proprietry object document.documentMode. By default this is set to isIE and overridden with the compatibility mode major version number when document.documentMode exists.
- Sniff!
http://dean.edwards.name/weblog/2007/03/sniff/
Dean Edward's blog post from 2007 where he used Conditional Compilation to show a super-short way of detecting whether the browser was IE or not, and comment 21 by Cristian Carlesso showing @jscriptversion to detect the version - Conditional Comments
http://en.wikipedia.org/wiki/Conditional_comment
Especially the section on Conditional Compilation near the bottom showing the version numbers given by @_jscript_version - Conditional Compilation (JavaScript)
http://msdn.microsoft.com/en-us/library/121hztk3%28v=vs.94%29.aspx
Kind of helped, in the way that it confirmed it would probably work ish - /*@cc_on and IE6 detection
http://stackoverflow.com/questions/1843247/cc-on-and-ie6-detection
The problem laid out in this stack was the problem I set about working around - Internet Explorer (IE) version detection in JavaScript
http://tanalin.com/en/articles/ie-version-js/
A brilliant write-up on the subject by Marat Tanalin, wish I'd seen this before
There was a stackoverflow answer that mentioned Conditional Compilation and this got me started on the above, I can't find the stack again but I thank that person.
Created 2013 December 12