简体   繁体   English

如何检测 Internet Explorer 11 及以下版本?

[英]How to detect Internet Explorer 11 and below versions?

I'm wondering how I could detect if the user viewing my website is using Internet Explorer 11 or below versions with Javascript.我想知道如何检测查看我网站的用户是否使用带有 Javascript 的 Internet Explorer 11 或更低版本。

It should be compatible and works with all these versions.它应该兼容并适用于所有这些版本。

How to achieve that ?如何做到这一点?

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");
    }
}

Check for documentmode - the IE only property:检查documentmode - IE 唯一的属性:

if (document.documentMode) 
    alert('this is IE');

I'd like to streamline the correct answer.我想简化正确的答案。 This returns true or false :这将返回truefalse

function is_IE() {
  return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}

Simpler version, returns boolean更简单的版本,返回布尔值

function isIE() {
  return !!window.navigator.userAgent.match(/MSIE|Trident/);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM