23

I'm wondering how I could detect if the user viewing my website is using Internet Explorer 11 or below versions with Javascript.

It should be compatible and works with all these versions.

How to achieve that ?

  • If the `string` `Trident` is in `window.navigator.userAgent`. – deEr. Apr 23 '18 at 17:26
  • @AjAX. Trident is only for IE11, older versions of IE used MSIE – Ryan Wilson Apr 23 '18 at 17:27
  • @RyanWilson Nope. It’s — not. `Trident` is in all down to `IE 6`. Both are. – deEr. Apr 23 '18 at 17:34
  • @AjAX ok. Was not aware of both being in there down to IE6, but still best to check for both in case some ancient computer is out there running older version than IE6. – Ryan Wilson Apr 23 '18 at 17:58
  • 1
    @DerekBrown , That's not correct because I don't want to detect only version 11 , I get the answer so thanks –  Apr 23 '18 at 18:13

4 Answers4

47

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");
    }
}
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
9

Check for documentmode - the IE only property:

if (document.documentMode) 
    alert('this is IE');
Andrei Karcheuski
  • 3,116
  • 3
  • 38
  • 39
8

I'd like to streamline the correct answer. This returns true or false:

function is_IE() {
  return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}
Timm
  • 2,488
  • 2
  • 22
  • 25
1

Simpler version, returns boolean

function isIE() {
  return !!window.navigator.userAgent.match(/MSIE|Trident/);
}
Will
  • 732
  • 9
  • 21