Internet Explorer 11 Detection

Internet Explorer 11 detection

Edit 18 Nov 2016

This code also work (for those who prefer another solution , without using ActiveX)

var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
// true on IE11
// false on Edge and other IEs/browsers.

Original Answer

In order to check Ie11 , you can use this : ( tested)

(or run this)

!(window.ActiveXObject) && "ActiveXObject" in window

I have all VMS of IE :

Sample Image

Sample Image

Sample Image

Sample Image

Notice : this wont work for IE11 :

as you can see here , it returns true :

Sample Image

So what can we do :

Apparently , they added the machine bit space :

ie11 :

"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"

ie12 :

"Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"

so we can do:

/x64|x32/ig.test(window.navigator.userAgent)

this will return true only for ie11.

How to detect Internet Explorer 11 and below versions?

Here you go, this should work for you:

//Per Icycool, one liner
//function isIE(){
// return window.navigator.userAgent.match(/(MSIE|Trident)/);
// }

function isIE() {
const ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
const msie = ua.indexOf('MSIE '); // IE 10 or older
const trident = ua.indexOf('Trident/'); //IE 11

return (msie > 0 || trident > 0);
}

//function to show alert if it's IE
function ShowIEAlert(){
if(isIE()){
alert("User is using IE");
}
}

How to detect IE11?

IE11 no longer reports as MSIE, according to this list of changes it's intentional to avoid mis-detection.

What you can do if you really want to know it's IE is to detect the Trident/ string in the user agent if navigator.appName returns Netscape, something like (the untested);

function getInternetExplorerVersion(){  var rv = -1;  if (navigator.appName == 'Microsoft Internet Explorer')  {    var ua = navigator.userAgent;    var re = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");    if (re.exec(ua) != null)      rv = parseFloat( RegExp.$1 );  }  else if (navigator.appName == 'Netscape')  {    var ua = navigator.userAgent;    var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\\.0-9]{0,})");    if (re.exec(ua) != null)      rv = parseFloat( RegExp.$1 );  }  return rv;}
console.log('IE version:', getInternetExplorerVersion());

Detecting IE version using CSS Capability/Feature Detection

So I found my own solution to this problem in the end.

After searching through Microsoft documentation I managed to find a new IE11 only style msTextCombineHorizontal

In my test, I check for IE10 styles and if they are a positive match, then I check for the IE11 only style. If I find it, then it's IE11+, if I don't, then it's IE10.

Code Example: Detect IE10 and IE11 by CSS Capability Testing (JSFiddle)

 /**  Target IE 10 with JavaScript and CSS property detection.    # 2013 by Tim Pietrusky  # timpietrusky.com **/
// IE 10 only CSS properties var ie10Styles = [ 'msTouchAction', 'msWrapFlow', 'msWrapMargin', 'msWrapThrough', 'msOverflowStyle', 'msScrollChaining', 'msScrollLimit', 'msScrollLimitXMin', 'msScrollLimitYMin', 'msScrollLimitXMax', 'msScrollLimitYMax', 'msScrollRails', 'msScrollSnapPointsX', 'msScrollSnapPointsY', 'msScrollSnapType', 'msScrollSnapX', 'msScrollSnapY', 'msScrollTranslation', 'msFlexbox', 'msFlex', 'msFlexOrder'];
var ie11Styles = [ 'msTextCombineHorizontal'];
/* * Test all IE only CSS properties */ var d = document; var b = d.body; var s = b.style; var ieVersion = null; var property;
// Test IE10 properties for (var i = 0; i < ie10Styles.length; i++) { property = ie10Styles[i];
if (s[property] != undefined) { ieVersion = "ie10"; createEl("IE10 style found: " + property); } }
// Test IE11 properties for (var i = 0; i < ie11Styles.length; i++) { property = ie11Styles[i];
if (s[property] != undefined) { ieVersion = "ie11"; createEl("IE11 style found: " + property); } }
if (ieVersion) { b.className = ieVersion; $('#versionId').html('Version: ' + ieVersion); } else { createEl('Not IE10 or 11.'); }
/* * Just a little helper to create a DOM element */ function createEl(content) { el = d.createElement('div'); el.innerHTML = content; b.appendChild(el); }
/* * List of IE CSS stuff: * http://msdn.microsoft.com/en-us/library/ie/hh869403(v=vs.85).aspx */
body {    font: 1.25em sans-serif;}div {    background: red;    color:#fff;    padding: 1em;}.ie10 div {    background: green;    margin-bottom:.5em;}.ie11 div {    background: purple;    margin-bottom:.5em;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><h1>Detect IE10 and IE11 by CSS Capability Testing</h1>

<h2 id="versionId"></h2>

Check if user is using IE

Use below JavaScript method :

function msieversion() 
{
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}

return false;
}

You may find the details on below Microsoft support site :

How to determine browser version from script

Update : (IE 11 support)

function msieversion() {

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}

return false;
}

Internet explorer 11 detection on server side

I solved this by using the Regex below after having a knock out system to check what browser is being used to access the site.

in this case, even if the browser "IE" is checked and returns false, I go ahead and use this regex and check to see if it is a match against the user agent:

(?:\b(MS)?IE\s+|\bTrident\/7\.0;.*\s+rv:)(\d+)

I hope this helps someone. I tested it and works fine. I also changed the rv to be 12 and upwards, and it works fine too in case if in IE12, they change rv to be 12.

How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?

I don't know why, but I'm not seeing "Edge" in the userAgent like everyone else is talking about, so I had to take another route that may help some people.

Instead of looking at the navigator.userAgent, I looked at navigator.appName to distinguish if it was IE<=10 or IE11 and Edge. IE11 and Edge use the appName of "Netscape", while every other iteration uses "Microsoft Internet Explorer".

After we determine that the browser is either IE11 or Edge, I then looked to navigator.appVersion. I noticed that in IE11 the string was rather long with a lot of information inside of it. I arbitrarily picked out the word "Trident", which is definitely not in the navigator.appVersion for Edge. Testing for this word allowed me to distinguish the two.

Below is a function that will return a numerical value of which Internet Explorer the user is on. If on Microsoft Edge it returns the number 12.

Good luck and I hope this helps!

function Check_Version(){
var rv = -1; // Return value assumes failure.

if (navigator.appName == 'Microsoft Internet Explorer'){

var ua = navigator.userAgent,
re = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");

if (re.exec(ua) !== null){
rv = parseFloat( RegExp.$1 );
}
}
else if(navigator.appName == "Netscape"){
/// in IE 11 the navigator.appVersion says 'trident'
/// in Edge the navigator.appVersion does not say trident
if(navigator.appVersion.indexOf('Trident') === -1) rv = 12;
else rv = 11;
}

return rv;
}

detect IE version and forbid the lower version doesn't work in IE11

It looks like you want to inform your site user to use the IE 11 browser if they are using an older version of the IE browser.

Sample code:

<!doctype html>
<html>
<head>
<title>
Test to detect IE browser
</title>

</head>
<body >
<div id="info"></div><br>
<h2>Test Page...</h2>

<script>
function Detect_IE() {
var ua = window.navigator.userAgent;

var msie = ua.indexOf('MSIE ');
if (msie > 0) {

return "IE " + parseInt( ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}

var trident = ua.indexOf('Trident/');
if (trident > 0) {

var rv = ua.indexOf('rv:');
return "IE " + parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}

// other browser
return "false";
}
var result=Detect_IE();
if (result=="false")
{
document.getElementById("info").innerHTML +="<h2>Not IE, any other browser....</h2>";
}
else if (result=="IE 11")
{
document.getElementById("info").innerHTML += "<h2>Dear user you are using " + result + ".</h2>";
}
else
{
document.getElementById("info").innerHTML += "<h2>Dear user you are using " + result + " This browser is outdated and not supported by this site. Kindly use supported browser...</h2>";
}
</script>
</body>
</html>

How to properly detect IE11 or later?

If only IE browsers are connecting to the page, then test for hoisted block-level function declaration as only supported in IE11+

function hoistTest() {    // Note: only available outside of strict mode.    { function f() { return 1; } }      function g() { return 1; }    { function g() { return 2; } }    { function h() { return 1; } }      function h() { return 2; }        return f() === 1 && g() === 2 && h() === 1;}
document.getElementById('out').appendChild(document.createTextNode('hoisted block-level function declaration: ' + hoistTest()));
<pre id="out"></pre>


Related Topics



Leave a reply



Submit