简体   繁体   English

检测Internet Explorer的任何和所有版本

[英]Detecting any and all versions of Internet Explorer

There used to be a nice way to tell if a web browser is IE or not, by using this technique in HTML: 通过在HTML中使用此技术,曾经有一种很好的方式来判断Web浏览器是否为IE:

<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->

or 要么

<!--[if !IE]-->
IE ignores this
<!--[endif]-->

but this doesn't work anymore in IE 10. 但这在IE 10 中不再起作用

Any idea what to use instead to tell IE from other web browsers (using HTML or JavaScript)? 不知道该用什么代替从其他网络浏览器(使用HTML或JavaScript)告诉IE?

PS. PS。 I need to be able to tell ANY version of IE from non-IE web browser. 我需要能够从非IE网络浏览器告诉任何版本的IE。

I appreciate all your insight, but none of it answers my actual question. 我很欣赏你的所有见解,但没有一个能回答我的实际问题。 Again, I am not asking about the feature detection . 同样,我不是在询问功能检测 All I need to know is if the web browser is IE or not. 我需要知道的是,网络浏览器是否是IE浏览器。 The following uses JavaScript and seems to work for all current versions of IE (including IE10): 以下使用JavaScript,似乎适用于所有当前版本的IE(包括IE10):

<![if IE]>
<script type='text/javascript'>
if(/*@cc_on!@*/false)
var bIsIE = 1;
</script>
<![endif]>

and then to check if it's IE, do this from JavaScript: 然后检查它是否是IE,从JavaScript执行此操作:

if (typeof (bIsIE) != 'undefined')
{
    //The web browser is IE
}
else
{
    //The web browser is not IE
}

Obviously the code above assumes that the web browser has JavaScript enabled. 显然,上面的代码假定Web浏览器启用了JavaScript。 But in my case the browser detection is relevant only if it has scripts enabled. 但就我而言,浏览器检测只有在启用了脚本时才有意义。

Every version of Internet Explorer is different from the others, just as every version of Chrome, Firefox, and Opera are different from their predecessors. 每个版本的Internet Explorer都与其他版本不同,就像Chrome,Firefox和Opera的每个版本都与其前辈不同。 You don't target vendors such as "Microsoft", "Google", or "Mozilla" when you develop websites—you target features. 在开发网站时,您不会针对“Microsoft”,“Google”或“Mozilla”等供应商 - 您定位功能。

Rather than asking " I'd like to use ::after, is this browser a Microsoft browser? " You should instead ask " Does this browser support pseudo-elements on the :: prefix? " This is feature-detection, and it's nearly always perfectly on target. 而不是问“ 我想使用::之后,这个浏览器是微软浏览器吗? ”你应该问“ 这个浏览器是否支持::前缀上的伪元素? ”这是特征检测,它几乎是永远完美的目标。 Rather than guessing what a browser is capable of by its vendor, you determine what it's capable of by what it can actually do. 而不是猜测其供应商的浏览器能力,您可以通过它实际可以做的事情来确定它的能力。

This may not be the answer you were looking for, but it's the correct answer nonetheless. 这可能不是您正在寻找的答案,但它仍然是正确的答案。 If you're asking how to identify all Microsoft browsers, you are approaching the problem (or what you perceive to be a problem) incorrectly. 如果您正在询问如何识别所有Microsoft浏览器,那么您正在错误地解决问题 (或您认为存在的问题)。

For proper solutions, I would encourage you to use tools like jQuery and Modernizr . 对于正确的解决方案,我建议您使用jQueryModernizr等工具。 These will handle API normalization, shimming of newer elements in older browsers, as well as feature-detection. 这些将处理API规范化,旧浏览器中较新元素的填充以及特征检测。 This is the correct way to do things, and had developers been taking this approach from the beginning you may not have such a distaste for Internet Explorer today. 这是正确的做事方式,并且开发人员从一开始就采用这种方法,你今天可能不会对Internet Explorer产生这样的厌恶。

The link you give in your question - doesn't work anymore - which is to Windows Internet Explorer Engineering Team Blog leads to the following statement 您在问题中提供的链接 - 不再起作用 - 这是Windows Internet Explorer工程团队博客导致以下声明

Conditional Comments 条件评论

 <!--[if IE]> This content is ignored in IE10 and other browsers. In older versions of IE it renders as part of the page. <![endif]--> 

This means conditional comments can still be used, but will only target older versions of IE. 这意味着仍然可以使用条件注释,但仅针对旧版本的IE。 If you need to distinguish between more recent browsers, use feature detection instead. 如果您需要区分更新的浏览器,请改用功能检测

It seems to me that the IE team are strongly pushing for the use of feature detection rather than browser detection as the quote from the feature detection link above shows. 在我看来,IE团队强烈推动使用特征检测而不是浏览器检测,因为上面的特征检测链接的引用显示。

Same Markup: Core Guidelines 相同标记:核心指南

 **DO** Feature Detection Test whether a browser supports a feature before using it. Behavior Detection Test for known issues before applying a workaround. **DON'T** Detect Specific Browsers Also known as browser detection. Don't use the identity of a browser (eg navigator.userAgent) to alter page behavior. Assume Unrelated Features Don't perform feature detection for one feature, and then proceed to use a different feature. 

So it appears that the Windows Internet Explorer Engineering Team are setting IE up so that you will not be able to use browser detection for IE10 and above. 因此,Windows Internet Explorer工程团队似乎正在设置IE,以便您无法对IE10及更高版本使用浏览器检测。

EDIT I do not use IE10 but does 编辑我不使用IE10,但确实如此

navigator.appName=='Microsoft Internet Explorer';

work in IE10? 在IE10工作?

It isn't enough to just say IE10 is good enough and ignore the problem. 仅仅说IE10足够好而忽略了这个问题是不够的。 It really depends on what you are trying to do. 这实际上取决于你想要做什么。 For most purposes feature detection would likely handle what you need. 对于大多数用途,功能检测可能会满足您的需求。 The far, far more complicated route is to start user agent detection by pulling in the user agent string from the HTTP request header. 更复杂的路线是通过从HTTP请求标头中提取用户代理字符串来启动用户代理检测。 If you aren't careful with this you can go wrong pretty quickly. 如果你不小心这个,你很快就会出错。

To view your current user agent string in a browser JS console: 要在浏览器JS控制台中查看当前的用户代理字符串:

console.log(navigator.userAgent);

Here is a list of reported user agent strings across all kinds of browsers: 以下是各种浏览器中报告的用户代理字符串列表:

http://www.zytrax.com/tech/web/browser_ids.htm http://www.zytrax.com/tech/web/browser_ids.htm

Note that all MS Explorer agent strings will contain "MSIE," but first you have to weed out browsers like Opera that will also include the "MSIE" string in some cases. 请注意,所有MS Explorer代理字符串都将包含“MSIE”,但首先您必须清除像Opera这样的浏览器,在某些情况下还会包含“MSIE”字符串。

This function returns true if the client browser is Internet Explorer , tested on versions 9-10-11 . 如果客户端浏览器是Internet Explorer ,在版本9-10-11上进行测试,则此函数返回true

function isIE(v) {
    var ie;
    ie = RegExp('msie' + (!isNaN(v)?('\\s'+v):''), 'i').test(navigator.userAgent);
    if (!ie) { ie = !!navigator.userAgent.match(/Trident.*rv[ :]*11\./) }
    return ie;
}

// Example
var ie = isIE();
var ie9 = isIE(9);
var ie10 = isIE(10);

NOTE: the function is incomplete and won't allow for isIE(11) 注意:该功能不完整,不允许使用isIE(11)

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

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