1

Does a reliable method for detecting IE browser version exist? So far I have tried.

  • IE Conditional comments, not reliable
  • User Agent HTTP request header, not always set

My next option was to try out javascript with something like

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);

But will javascript always have access to this information?

PDStat
  • 5,513
  • 10
  • 51
  • 86
  • Yes, javascript will have access to this info unless user disables js on their browser. If a user disables js, most sites would not work correctly. There are also questions on detecting IE11 such as this one https://stackoverflow.com/questions/21825157/internet-explorer-11-detection – Huangism Apr 04 '19 at 14:07
  • 3
    Just a side note: Have you considered doing [feature detection](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Feature_detection) instead of platform detection? – Sam Herrmann Apr 04 '19 at 14:13
  • Do you need to distinguish IE11's compatibility modes? eg if IE11 is in IE8-compatibility mode, how would you want to detect that? – Spudley Apr 04 '19 at 14:27
  • I have a requirement to render a "Browser not supported" message if IE < 11. And not render/show the rest of the page. So yes even in compatibility mode of IE < 11 I should show this message – PDStat Apr 04 '19 at 14:31

2 Answers2

0

One of the main features (although poorly implemented) that exists in IE11 and not earlier versions is const. So you could just create a const variable in an eval and see if an error is being thrown.

function isIE11() {
  try {
    eval("const x = 1;");
    return true;
  } catch (err) {}
  return false;
}
console.log(isIE11());
nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

You can try to refer code example below which can identify IE7, IE8, IE9, IE10, IE11 versions.

<!DOCTYPE html>
<html>
<head>
 <title>Page Title</title>
 <script>
 function GetIEVersion() {
  var sAgent = window.navigator.userAgent;
  var Idx = sAgent.indexOf("MSIE");

  // If IE, return version number.
  if (Idx > 0) 
    return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));

  // If IE 11 then look for Updated user agent string.
  else if (!!navigator.userAgent.match(/Trident\/7\./)) 
    return 11;

  else
    return 0; //It is not IE
}

if (GetIEVersion() > 0) 
   alert("This is IE " + GetIEVersion());
else 
   alert("This is not IE.");
GetIEVersion();
 </script>
</head>
<body>



</body>
</html>

Further, You can try to modify this code as per your own requirement.

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19