How to detect Internet Explorer 11 and below versions?

后端 未结 4 1446
感动是毒
感动是毒 2020-12-29 08:02

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

相关标签:
4条回答
  • 2020-12-29 08:08

    Check for documentmode - the IE only property:

    if (document.documentMode) 
        alert('this is IE');
    
    0 讨论(0)
  • 2020-12-29 08:26

    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");
        }
    }
    
    0 讨论(0)
  • 2020-12-29 08:26

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

    function is_IE() {
      return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
    }
    
    0 讨论(0)
  • 2020-12-29 08:29

    Simpler version, returns boolean

    function isIE() {
      return !!window.navigator.userAgent.match(/MSIE|Trident/);
    }
    
    0 讨论(0)
提交回复
热议问题