浏览器对象全检测


Javascript window对象下面有个对象: 
    Navigator 包含几个有用的属性:
         appName      浏览器类型
         appVersion  前浏览器代理版本,注意并不是浏览器版本
         appCodeName   浏览器代码名称,全兼容,所有已Netscape的浏览器中都是
Mozilla,说白了就是不变,不变他娘的就是没用!
         platform             
平台也就是当前使用的系统
         cookieEnabled  是否使用了cookie  返回值是布尔值
         userAgent          浏览器用户代理报头信息

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 32位系统 ie8截图: 其他浏览器都类似
浏览器对象全检测 - 眷恋天空的驴 - 眷恋天空的驴
 
1: 简单区分现代浏览器(注意:他娘的ie11属于现代浏览器不是ie浏览器系列)跟一些ie浏览器(指的是ie10以下全称为ie浏览器)

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浏览器
}

经测试,没有问题,测试环境win8 所有浏览器

2. 有些时候总是他娘的不需要这么多的垃圾代码,而只是hack写,so:
   a:  一般来说,我们是建议使用条件注释来替换浏览器检测的方法。本文所讲的技术仅仅在条件注释无法实现,或者  需要检测一个bug

<!--[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,而不关心浏览器版本,那只需要在调用函数的时候,不传递参数即可。

var ie  = isIE()

最后依次贴下在各大浏览器里测试代码的截图。

alert('ie6:' + isIE(6) + '\n' + 'ie7:' + isIE(7) + '\n' + 'ie8:' + isIE(8) + '\n' + 'ie9:' +

isIE(9) + '\n' + 'ie:' + isIE())


ie版本代码总结:

IE11或者非IE

if (!document.all) {
alert('IE11+ or not IE');
}

IE10

if (document.all && document.addEventListener && window.atob) {
alert('IE10');
}

IE9

if (document.all && document.addEventListener && !window.atob) {
alert('IE9');
}

IE8

if (document.all && document.querySelector && !document.addEventListener) {
alert('IE8');
}

IE7

if (document.all && window.XMLHttpRequest && !document.querySelector) {
alert('IE7');
}

IE7

if (document.all && document.compatMode && !window.XMLHttpRequest) {
alert('IE6');
}


大招:相比上面封装函数 myBrowser这个靠谱多了,毕竟是人老外他娘写的。

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;
})();


总结: 随着浏览器的进化,目前接触的项目已经变成了ie8,兼容还是比较好写的。
ps:      这些都是网上趴来的,东一榔头西一锤子。方便在自己以后查询。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值