1

What is the best way to identify IE 11 browser?

The format of the UA for IE 11 is : Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

I'm using the following code but I need to know if it is perfectly safe to use or is there any alternate approach too?

 sUserAgent = Request.ServerVariables("HTTP_USER_AGENT")
comstart = instr(sUserAgent,"(")
comend = instr(sUserAgent,")")
if comstart <> 0 then
    cmtlist = mid(sUserAgent,comstart+1,comend-comstart-1)
else
    cmtlist = " ; ; ;"
end if
cmtArray = split(cmtlist,";")
if instr(sUserAgent,"rv") > 0 then ' it's a Microsoft browser
    browserName="MSIE"
    browserVersion=right(cmtArray(1),len(cmtArray(1))-5)
endif

Also in some other file I'm using the following JavaScript code. Please advice if using this code is advisable or not. If there is a better approach , please suggest. Can this code give some unexpected results in the future? :

// In IE11, the true version is after "rv" in the User Agent string
    var nAgt = navigator.userAgent;
    if ((verOffset = nAgt.indexOf("rv")) != -1) 
    {
        browserName = "Microsoft Internet Explorer";
        fullVersion = nAgt.substring(verOffset + 3);

    }
nobody
  • 19,814
  • 17
  • 56
  • 77
Abhay
  • 46
  • 1
  • 9
  • Manybe duplicate http://stackoverflow.com/questions/14365725/easiest-lightest-replacement-for-browser-detection-jquery-1-9 – holmes2136 May 20 '14 at 15:12
  • As a general rule it's better to do feature detection than trying to identify browsers compatibility by user agents. You might be interested in http://modernizr.com/ – Mansueli May 20 '14 at 15:28

1 Answers1

0

While feature detection can be very useful, and is sometimes the quickest way to distinguish between supportive and non-supportive browser versions, detecting IE and its versions is actually a piece of cake:

var uA = navigator.userAgent;
var browser = null;
var ieVersion = null;
var htmlTag = document.documentElement;

if (uA.indexOf('MSIE 6') >= 0) {
    browser = 'IE';
    ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
    browser = 'IE';
    ieVersion = 7;
}
if (document.documentMode) { // as of IE8; strictly IE proprietary 
    browser = 'IE';
    ieVersion = document.documentMode;
}

// Using it can be done like this: 
if (browser == 'IE' && ieVersion == 11)
    htmlTag.className += ' ie11';

.
That's all you need. You're catching higher IEs in lower Modes/Views as well with this script, including Compatibility ~.