Internet Explorer 11 detection
JavascriptInternet ExplorerInternet Explorer-11Javascript Problem Overview
I know IE 11 has different user agent string than all other IE
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
I have tried to detect IE 11 with answer specified for this question'
https://stackoverflow.com/questions/18684099/jquery-fail-to-detect-ie-11
Thats !!navigator.userAgent.match(/Trident\/7\./)
But I am getting error
Object not found and needs to be re-evaluated.
Then I openede developer console in IE11 and tried to access some predefined javascript objects, I am still getting same error.
I have tried
navigator.userAgent
window.navigator
console.log('test');
Anyone have any idea about it ?
Javascript Solutions
Solution 1 - Javascript
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 :
Notice : this wont work for IE11 :
as you can see here , it returns true :
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.
Solution 2 - Javascript
To detect MSIE (from version 6 to 11) quickly:
if(navigator.userAgent.indexOf('MSIE')!==-1
|| navigator.appVersion.indexOf('Trident/') > -1){
/* Microsoft Internet Explorer detected in. */
}
Solution 3 - Javascript
I use the following function to detect version 9, 10 and 11 of IE:
function ieVersion() {
var ua = window.navigator.userAgent;
if (ua.indexOf("Trident/7.0") > -1)
return 11;
else if (ua.indexOf("Trident/6.0") > -1)
return 10;
else if (ua.indexOf("Trident/5.0") > -1)
return 9;
else
return 0; // not IE9, 10 or 11
}
Solution 4 - Javascript
All of the above answers ignore the fact that you mention you have no window or navigator :-)
> Then I openede developer console in IE11
and thats where it says
> Object not found and needs to be re-evaluated.
and navigator, window, console, none of them exist and need to be re-evaluated. I've had that in emulation. just close and open the console a few times.
Solution 5 - Javascript
A pretty safe & concise way to detect IE 11 only is
if(window.msCrypto) {
// I'm IE11 for sure
}
or something like this
var IE11= !!window.msCrypto;
msCrypto
is a prefixed version of the window.crypto
object and only implemented in IE 11.
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
Solution 6 - Javascript
Okay try this, simple and for IE11 and IE below 11 version
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;
navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1
for IE 11 version
navigator.userAgent.toUpperCase().indexOf("MSIE") != -1
for IE below 11 version
browserIsIE = navigator.userAgent.toUpperCase().indexOf("TRIDENT/") != -1 || navigator.userAgent.toUpperCase().indexOf("MSIE") != -1;
console.log('Is IE Browser : '+ browserIsIE)
Solution 7 - Javascript
And how I implemented this
<script type="text/javascript">
!(window.ActiveXObject) && "ActiveXObject"
function isIE11(){
return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
}
</script>
Solution 8 - Javascript
This link was helpful . It contains the javascript code to detect all versions of IE up to IE11. I tested the script with IE11 emulator. To find the IE11 emulator, right-click on the web browser click "Inspect element". At the bottom-left of the page, scroll down the navigation bar and click the desktop icon. The "User Agent String" dropdown box contains options to emulate IE6-11.
It works. I just used it some minutes before writing this answer. Cannot post snapshots - not enough reputation.
This is the code - follow the link to view it again:
// Get IE or Edge browser version
var version = detectIE();
if (version === false) {
document.getElementById('result').innerHTML = '<s>IE/Edge</s>';
} else if (version >= 12) {
document.getElementById('result').innerHTML = 'Edge ' + version;
} else {
document.getElementById('result').innerHTML = 'IE ' + version;
}
// add details to debug result
document.getElementById('details').innerHTML = window.navigator.userAgent;
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// Edge 12 (Spartan)
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge 13
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300);
body {
color: black;
background-color: white;
font-family: "Fira Sans", sans-serif;
font-weight: 300;
margin: 0;
padding: 3rem;
}
h1 {
color: darkgrey;
text-align: center;
font-weight: 300;
font-size: 1.5rem;
line-height: 2rem;
}
h2 {
text-align: center;
font-weight: 300;
font-size: 4rem;
}
p {
color: darkgrey;
text-align: center;
font-family: "Fira Mono", monospace;
font-size: 1rem;
line-height: 1.5rem;
}
<h1>Detect IE/Edge version with JavaScript.<br> Updated to recognize Internet Explorer 12+ aka Edge.</h1>
<h2 id="result">detecting…</h2>
<p id="details">n/a</p>
Solution 9 - Javascript
Using this RegExp seems works for IE 10 and IE 11:
function isIE(){
return /Trident\/|MSIE/.test(window.navigator.userAgent);
}
I do not have a IE older than IE 10 to test this.
Solution 10 - Javascript
Use Navigator:-
The navigator
is an object that contains all information about the client machine's browser.
navigator.appName
returns the name of the client machine's browser.
navigator.appName === 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie === 1) ? alert("Please dont use IE.") : alert("This is not IE")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Solution 11 - Javascript
I found IE11 is giving more than one user agent strings in different environments.
Instead of relying on MSIE
, and other approaches, It's better to rely on Trident
version
const isIE11 = userAgent => userAgent.match(/Trident\/([\d.]+)/) ? +userAgent.match(/Trident\/([\d.]+)/)[1] >= 7;
Hope this helps :)
Solution 12 - Javascript
For a minimal approach and untill IE officially dies on the August 17th 2021 拾✌️ I'm using the IE conditional statement in reverse <!--[if !IE]> --><!-- <![endif]-->
.
<style>
:root{display:none!important}
</style>
<!--[if !IE]> -->
<style>
:root{display:initial!important}
</style>
<!-- <![endif]-->