如何使用 JavaScript 检测 Internet Explorer (IE) 和 Microsoft Edge? [英] How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?

查看:36
本文介绍了如何使用 JavaScript 检测 Internet Explorer (IE) 和 Microsoft Edge?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,我知道有很多方法可以检测 Internet Explorer.

I've looked around a lot, and I understand that there's a lot of ways to detect internet explorer.

我的问题是:我的 HTML 文档中有一个区域,单击该区域时会调用与任何类型的 Internet Explorer 不兼容的 JavaScript 函数.我想检测是否正在使用 IE,如果是,将变量设置为 true.

My problem is this: I have an area on my HTML document, that when clicked, calls a JavaScript function that is incompatible with internet explorer of any kind. I want to detect if IE is being used, and if so, set the variable to true.

问题是,我正在用 Notepad++ 编写代码,当我在浏览器中运行 HTML 代码时,检测 IE 的方法均无效.我认为问题在于我正在用 Notepad++ 运行它.我需要能够检测 IE,以便基于变量,我可以禁用该站点的区域.我试过这个:

The problem is, I am writing my code out of Notepad++, and when I run the HTML code in browser, none of the methods for detecting IE work. I think the problem is that I am running it out of Notepad++. I need to be able to detect IE, so that based on the variable, I can disable that area of the site. I have tried this:

var isIE10 = false;

if (navigator.userAgent.indexOf("MSIE 10") > -1) {
    // this is internet explorer 10
    isIE10 = true;
   window.alert(isIE10);
}

var isIE = (navigator.userAgent.indexOf("MSIE") != -1);

if(isIE){
    if(!isIE10){
    window.location = 'pages/core/ie.htm';
    }
}

但它不起作用.如何从 Notepad++ 中检测 IE?这就是我用来测试 HTML 的内容,但我需要一种方法来处理它.

but it doesn't work. How can I detect IE out of Notepad++? That's what I'm testing the HTML out of, but I need a method that'll work with that.

我注意到有人将此标记为重复,这是可以理解的.我想我没有说清楚.我不能使用 JQuery 答案,所以这不是重复的,因为我要求的是普通的 JS 答案.

I noticed someone has marked this as a duplicate, and that is understandable. I suppose I was not clear. I cannot use a JQuery answer, so this is not a duplicate as I am asking for a vanilla JS answer.

是否也有检测 Microsoft Edge 浏览器的方法?

Is there also a way to detect the Microsoft Edge browser?

推荐答案

我不知道为什么,但我没有像其他人所说的那样在 userAgent 中看到Edge",所以我不得不另辟蹊径这可能会帮助一些人.

I don't know why, but I'm not seeing "Edge" in the userAgent like everyone else is talking about, so I had to take another route that may help some people.

我没有看 navigator.userAgent,而是看 navigator.appName 来区分是 IE<=10 还是 IE11 和 Edge.IE11 和 Edge 使用Netscape"的 appName,而其他所有迭代都使用Microsoft Internet Explorer".

Instead of looking at the navigator.userAgent, I looked at navigator.appName to distinguish if it was IE<=10 or IE11 and Edge. IE11 and Edge use the appName of "Netscape", while every other iteration uses "Microsoft Internet Explorer".

在我们确定浏览器是 IE11 或 Edge 后,我查看了 navigator.appVersion.我注意到在 IE11 中,字符串很长,里面有很多信息.我随意挑出了三叉戟"这个词,Edge 的 navigator.appVersion 里肯定没有这个词.测试这个词让我能够区分这两者.

After we determine that the browser is either IE11 or Edge, I then looked to navigator.appVersion. I noticed that in IE11 the string was rather long with a lot of information inside of it. I arbitrarily picked out the word "Trident", which is definitely not in the navigator.appVersion for Edge. Testing for this word allowed me to distinguish the two.

下面是一个函数,它将返回用户使用的 Internet Explorer 的数值.如果在 Microsoft Edge 上,则返回数字 12.

Below is a function that will return a numerical value of which Internet Explorer the user is on. If on Microsoft Edge it returns the number 12.

祝你好运,我希望这会有所帮助!

Good luck and I hope this helps!

function Check_Version(){
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer'){

       var ua = navigator.userAgent,
           re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

       if (re.exec(ua) !== null){
         rv = parseFloat( RegExp.$1 );
       }
    }
    else if(navigator.appName == "Netscape"){                       
       /// in IE 11 the navigator.appVersion says 'trident'
       /// in Edge the navigator.appVersion does not say trident
       if(navigator.appVersion.indexOf('Trident') === -1) rv = 12;
       else rv = 11;
    }       

    return rv;          
}

这篇关于如何使用 JavaScript 检测 Internet Explorer (IE) 和 Microsoft Edge?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆