为什么使用window.navigator.userAgent检索浏览器Explorer 11会被识别为Mozilla?
Why using window.navigator.userAgent to retrieve the browser Explorer 11 is recognized as Mozilla? How retrieve the user angent and version?
我是JavaScript和JQuery的新手,我正在疯狂地尝试实现一个简单的脚本来检测浏览器是否为Internet Explorer及其版本。
所以我要这样做:
$( document ).ready(function() {
//alert(navigator.appName);
//alert(navigator.appCodeName);
//alert(navigator.appVersion);
//alert(navigator.platform);
//alert(window.navigator.userAgent);
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
console.log("USER AGENT: " + ua);
console.log("MSIE: " + msie);
});
问题是,运行页面进入Explorer 11进入控制台日志,我获得以下消息:
USER AGENT: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; GWX:QUALIFIED; ASU2JS; rv:11.0) like Gecko
MSIE: -1
怎么可能它被识别为Mozilla而不是IE?
我需要识别浏览器是否是Internet Explorer及其版本。
如何执行此操作?
根据微软的说法,IE11的用户代理字符串是专门设计来欺骗UA解析器将其识别为其他内容的。来源:http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx
我将重复这一点,即使它在上面的文章中提到。如果你想做UA嗅探,请三思。特性检测是处理浏览器兼容性的推荐方法。
IE 11的发布破坏了所有客户端检查脚本。正如您所说,它现在报告为"Mozilla",不再报告MSIE。据我所知,他们之所以这么做是因为IE11应该更多地基于Gecko引擎,而不是Mozilla。为了说明这一点,微软决定将User-Agent字符串更改为不同的内容。我知道测试IE11的最好方法是检查"Trident/7.0",这应该是所有IE11浏览器都通知的。
Mozilla/5.0 (Windows NT 6.1;WOW64;三叉戟/7.0 ;rv:11.0) like Gecko
无论如何,正如许多人建议的那样,最好检查功能,而不是依赖于浏览器检查。
请尝试以下代码
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
var rv = -1;
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv':11'./)) // If Internet Explorer, return version number
{
if (isNaN(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))))) {
//For IE 11 >
if (navigator.appName == 'Netscape') {
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}['.0-9]{0,})");
if (re.exec(ua) != null) {
rv = parseFloat(RegExp.$1);
alert(rv);
}
}
else {
alert('otherbrowser');
}
}
else {
//For < IE11
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
return false;
}}
- 如何识别我的网站中的慢速设备
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- AngularJS指令只识别双向绑定类型
- php脚本中无法识别ajax传递的值
- 识别没有ID或特定属性的对象
- JavaScript无法识别图像
- Socket.io on Mozilla Rhino
- Mozilla没有;t在附加视频src后触发视频后台事件
- 如何动态检测Mozilla/Chrome上是否启用SSO
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- 识别由不同数量的项组成的json对象
- 无法识别从php创建的Js函数
- 错误:语法错误,无法识别的表达式:不支持的伪:无效/RectJS/Unit Testing
- 默认光标在IE和Mozilla中不起作用
- 函数无法识别初始化的变量
- 具有带大括号的字符串时无法识别的表达式
- 在Fiddler JavaScript中识别POST请求
- 无法识别 Mozilla Firefox 中的右键单击事件
- 为什么使用window.navigator.userAgent检索浏览器Explorer 11会被识别为Mozilla?