appCodeName 浏览器代码名称,全兼容,所有已Netscape的浏览器中都是 Mozilla,说白了就是不变,不变他娘的就是没用!
platform 平台也就是当前使用的系统
cookieEnabled 是否使用了cookie 返回值是布尔值
userAgent 浏览器用户代理报头信息
运行代码: win8 32位系统 ie8截图: 其他浏览器都类似document.write("<p>浏览器:")
document.write(navigator.appName + "</p>")
document.write("<p>浏览器版本:")
document.write(navigator.appVersion + "</p>")
document.write("<p>代码:")
document.write(navigator.appCodeName + "</p>")
document.write("<p>平台:")
document.write(navigator.platform + "</p>")
document.write("<p>Cookies 启用:")
document.write(navigator.cookieEnabled + "</p>")
document.write("<p>浏览器的用户代理报头:")
document.write(navigator.userAgent + "</p>")
经测试,没有问题,测试环境win8 所有浏览器function myBrowser(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1;
if (isOpera) {
return "Opera"
}; //判断是否Opera浏览器
if (userAgent.indexOf("Firefox") > -1) {
return "FF";
} //判断是否Firefox浏览器
if (userAgent.indexOf("Chrome") > -1){
return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
return "Safari";
} //判断是否Safari浏览器
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
return "IE";
}; //判断是否IE浏览器
}
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->>
IE10及其以下的浏览器可以通过检测存在非标准的 document.all 对象来区分
因此检测如下所示:
<script>
var ie = false;
</script>
<!--[if lte IE 7]><script>
ie = 7;
</script><![endif]-->
更进一步 // 注释: 改该函数自测发现不起作用,后续修复,mark一下。
var isIE = function(ver){
var b = document.createElement('b')
b.innerHTML = '<!--[if IE ' + ver + ']>1<![endif]-->'
return b.innerHTML === 1
}
if(isIE(6)){
// IE 6
}
// ...
if(isIE(9)){
// IE 9
}
这样想检测哪个版本都毫无压力。但是,如果只想检测是不是IE,而不关心浏览器版本,那只需要在调用函数的时候,不传递参数即可。原文来自:http://caibaojian.com/detect-ie-version.html
var ie = isIE()
最后依次贴下在各大浏览器里测试代码的截图。
alert('ie6:' + isIE(6) + '\n' + 'ie7:' + isIE(7) + '\n' + 'ie8:' + isIE(8) + '\n' + 'ie9:' +
isIE(9) + '\n' + 'ie:' + isIE())
IE10if (!document.all) {
alert('IE11+ or not IE');
}
if (document.all && document.addEventListener && window.atob) {
alert('IE10');
}
if (document.all && document.addEventListener && !window.atob) {
alert('IE9');
}
if (document.all && document.querySelector && !document.addEventListener) {
alert('IE8');
}
if (document.all && window.XMLHttpRequest && !document.querySelector) {
alert('IE7');
}
if (document.all && document.compatMode && !window.XMLHttpRequest) {
alert('IE6');
}
var win = window;
var doc = win.document;
var input = doc.createElement ("input");
var ie = (function (){
//"!win.ActiveXObject" is evaluated to true in IE11
if (win.ActiveXObject === undefined) return null;
if (!win.XMLHttpRequest) return 6;
if (!doc.querySelector) return 7;
if (!doc.addEventListener) return 8;
if (!win.atob) return 9;
//"!doc.body.dataset" is faster but the body is null when the DOM is not
//ready. Anyway, an input tag needs to be created to check if IE is being
//emulated
if (!input.dataset) return 10;
return 11;
})();