-4

i am checking a script for IE11, but the following does not seems to be working, it returns me false.

function isIE () {
            var myNav = navigator.userAgent.toLowerCase();
    alert(myNav);
            return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
        }
alert(isIE());

it does seems to find msie

can anyone update this function to detect IE11

Chris Diver
  • 19,362
  • 4
  • 47
  • 58
  • 2
    Why browser sniffing? – Ram Jun 10 '15 at 20:23
  • Duplicate question: http://stackoverflow.com/questions/21204017/html-css-detect-ie11-without-javascript Also, see [Microsoft's advice](https://msdn.microsoft.com/library/hh801214(v=vs.85).aspx) for handling IE10+. – Patrick Roberts Jun 10 '15 at 20:25

2 Answers2

1

Checking for a specific browser isn't recommended any more - feature detection is generally more useful. If you really need to detect IE11, the following page has an example script under the section on detecting IE x.x:

http://www.javascriptkit.com/javatutors/navigator.shtml

In case this link disappears in future, the script is as follows:

//userAgent in IE7 WinXP returns: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
//userAgent in IE11 Win7 returns: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

if (navigator.userAgent.indexOf('MSIE') != -1)
 var detectIEregexp = /MSIE (\d+\.\d+);/ //test for MSIE x.x
else // if no "MSIE" string in userAgent
 var detectIEregexp = /Trident.*rv[ :]*(\d+\.\d+)/ //test for rv:x.x or rv x.x where Trident string exists

if (detectIEregexp.test(navigator.userAgent)){ //if some form of IE
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion>=12)
  document.write("You're using IE12 or above")
 else if (ieversion>=11)
  document.write("You're using IE11 or above")
 else if (ieversion>=10)
  document.write("You're using IE10 or above")
 else if (ieversion>=9)
  document.write("You're using IE9 or above")
 else if (ieversion>=8)
  document.write("You're using IE8 or above")
 else if (ieversion>=7)
  document.write("You're using IE7.x")
 else if (ieversion>=6)
  document.write("You're using IE6.x")
 else if (ieversion>=5)
  document.write("You're using IE5.x")
}
else{
 document.write("n/a")
}

So to replace your function, you could use something like:

function isIE() {

    if (navigator.userAgent.indexOf('MSIE') != -1)
        return true;

    var detectIEregexp = /Trident.*rv[ :]*(\d+\.\d+)/   
    return detectIEregexp.test(navigator.userAgent);
}
Alan
  • 2,962
  • 2
  • 15
  • 18
0
var myNav = navigator.userAgent;

For IE10 and below

myNav.indexOf('MSIE ') != -1

For IE11

myNav.indexOf('Trident/') != -1

For IE12

myNav.indexOf('Edge/') != -1

Your updated function will be like this

function isIE () {
    var myNav = navigator.userAgent;
    return (myNav.indexOf('MSIE ') != -1 || myNav.indexOf('Trident/') != -1 || myNav.indexOf('Edge/') != -1);
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59