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

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

问题描述

我周围环顾了很多,我明白有很多方法可以检测Internet Explorer。我的问题是这样的:我在HTML文档上有一个区域,点击时调用与任何类型的Internet Explorer不兼容的JavaScript函数。我希望检测是否正在使用IE,如果是,请将变量设置为true。问题是,我正在使用记事本++编写我的代码,并且当我在浏览器中运行HTML代码时,没有任何方法可以检测IE炒锅。我认为问题在于我正在使用记事本++。我需要能够检测IE,所以基于变量,我可以禁用该网站的该区域。我试过这个:

  / *在这里写JavaScript * / 
var isIE10 = false;
$ b $ if(navigator.userAgent.indexOf(MSIE 10)> -1){
//这是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';
}
}

但它不起作用。我如何检测IE记事本++?这就是我测试HTML的原因,但我需要一种可以在网站上使用的方法。帮助!

编辑



我注意到有人将此标记为重复的,这是可以理解的。我想我不清楚。我不能使用JQuery的答案,所以这不是一个重复的,因为我要求一个香草JS的答案。

编辑#2还有一种方法来检测边缘浏览器?


解决方案

我不知道为什么,但我没有像其他人一样在userAgent中看到边缘谈论,所以我不得不采取另一条路线,可能会帮助一些人。



而不是看着navigator.userAgent,我看着navigator.appName来区分是否它是IE <= 10或IE11和Edge。 IE11和Edge使用Netscape的appName,而其他所有迭代都使用Microsoft Internet Explorer。

在我们确定浏览器是IE11或Edge之后,我再看看navigator.appVersion。我注意到在IE11中,字符串很长,其中有很多信息。我随意挑选了三叉戟这个词,它绝对不在导航器的边缘.app版本中。测试这个词允许我区分这两个。



下面是一个函数,它将返回用户所在的Internet Explorer的数值。

祝你好运,我希望这有助于你!

 函数Check_Version(){
var rv = -1; //返回值假定失败。

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){
///在IE 11中navigator.appVersion表示'trident'
/ //在Edge中navigator.appVersion不会说三叉戟
if(navigator.appVersion.indexOf('Trident')=== -1)rv = 12;
else rv = 11;
}

return rv;
}


I have looked around a lot, and I understand that there are a lot of ways to detect internet explorer. My problem is this: I have an area on my HTML doc, that when clicked, calls a javascript function that is incompatible with internet explorer of any kind. I wish to detect if IE is being used, and if so, set a variable as true. The problem is, I am writing my code out of notepad ++, and when I run the HTML code in browser, none of the methods to detect IE wok. 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:

/* Write JavaScript here */
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';
    }
}

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 and on a website. HELP!

edit

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

Edit #2 is there also a way to detect the edge browser?

解决方案

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.

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".

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.

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天全站免登陆