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

Javascript 如何检测Internet Explorer 11及以下版本?,javascript,internet-explorer,Javascript,Internet Explorer,我想知道如何检测查看我的网站的用户是否使用带有Javascript的InternetExplorer11或更低版本 它应该兼容所有这些版本。 如何做到这一点?给你,这应该对你有用: //Per Icycool, one liner //function isIE(){ // return window.navigator.userAgent.match(/(MSIE|Trident)/); // } function isIE() {

我想知道如何检测查看我的网站的用户是否使用带有Javascript的InternetExplorer11或更低版本

它应该兼容所有这些版本。


如何做到这一点?

给你,这应该对你有用:

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

我想简化正确的答案。这将返回
true
false

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

检查documentmode-仅IE属性:

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

更简单的版本,返回布尔值

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

如果
字符串
Trident
位于
窗口.navigator.userAgent
@AjAX中。Trident仅适用于IE11,使用的是旧版本的IEMSIE@RyanWilson不。不是<代码>三叉戟
的全部功能是
IE 6
。两者都很好。我不知道这两个版本都存在于IE6中,但还是最好检查一下,以防某些旧计算机运行的版本比IE6旧。@DerekBrown,这是不正确的,因为我不想只检测版本11,所以我得到了答案,谢谢
window.navigator.userAgent.match(/(MSIE | Trident)/)
如果您想在1中完成此操作line@Timm嗯,这只是使用字符串模式的老习惯,以防正则表达式中有其他部分。在这种情况下,括号是正确的。如果希望与旧版本的IE兼容,请使用
var
not
const
!谢谢。救了我day@Siva-不客气。很高兴提供帮助:)FYI
/MSIE | Trident/.test(window.navigator.userAgent)
返回bool。