In order to detect IE most Javascript libaries do all sort of tricks.
- jQuery seem to add a temporary object into your pages's DOM to detect some features,
- YUI2 does regex on the user agent in its
YAHOO.env.ua = function()
(fileyahoo.js
)
After reading this answer it came in my mind that it's true, in order to detect simply IE in Javascript we could simply add to our pages:
<!--[if IE]><script type="text/javascript">window['isIE'] = true;</script><![endif]-->
<script type="text/javascript" src="all-your-other-scripts-here.js"></script>
Now the window.isIE
variable is set for all our Javascript code, by simply doing:
if(window.isIE)
...
Beside the fact that this might result in being a pain because it has to be added in all pages, are there any issues/considerations I might be unaware of?
FYI: I know it's better to use object detection rather than browser detection, but there are cases where you still have to use browser detection.
navigator.userAgent
exists if browser detection (rather than feature detection) is really needed, and jQuery uses it to get the information for the$.browser
object. It's much nicer than having to include an IE-specific conditional comment in every page.IE 11 has changed a lot and now many past methods of browser detection do not work. The below code works for IE 11 and earlier.
I think I have what you are looking for. You can get the Full Version of Internet Explorer as a string "AA.BB.CCCC.DDDD" using Javascript and clientCaps.
http://www.pinlady.net/PluginDetect/IE/
It appears to work for IE 5.5 and higher (including IE 10). It is immune to the navigator.userAgent/document mode/browser mode. There is no need for conditional comments, or any extra HTML elements. It is a pure Javascript solution.
I am not certain at this time how IE Mobile behaves, but you can always use a backup detection method in case this clientCaps method fails.
So far, I gotta say, it works pretty well.
something quick I wrote quick after looking at this question in case anyone wants it.
** EDIT **
Per Johnny Darvall's comment below, I'm adding a link for anyone who is trying to sniff out Internet Explorer 11:
http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx
This works quite well,
var isIe = !!window.ActiveXObject;
Here you can find some really simple hacks for browser-detecting: http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/