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

新手上路,请多包涵

我想知道如何检测查看我网站的用户是否使用 Internet Explorer 11 或以下版本的 Javascript。

它应该兼容并适用于所有这些版本。

如何实现?

原文由 user9678848 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 265
2 个回答

给你,这应该适合你:

 //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 发布,翻译遵循 CC BY-SA 4.0 许可协议

检查 documentmode - IE 唯一属性:

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

原文由 Andrei Karcheuski 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题