• 有趣的浏览器检测


    原文:http://snandy.iteye.com/blog/603712

    在写跨浏览器的js程序中,检测浏览器是一个很重要的工作。我们不时要为不同的浏览器写分支代码。如下是一种:

    Js代码  收藏代码
    1. //添加事件工具函数  
    2. function addEvent(el,type,handle){  
    3.     if(el.addEventListener){//for standard browses  
    4.         el.addEventListener(type,handle,false);  
    5.     }else if(el.attachEvent){//for IE  
    6.         el.attachEvent("on"+event,handle);  
    7.     }else{//other  
    8.         el["on"+type]=handle;  
    9.     }  
    10.   
    11. }  

     

    1,第一种检测浏览器方式称为 user-agent 检测方式。是最古老的,它检测目标浏览器的确切型号,包括浏览器的名称和版本。其实就是一个字符串,用navigator.userAgen或navigator.appName获取。如下:

    Js代码  收藏代码
    1. function isIE(){  
    2.     return navigator.appName.indexOf("Microsoft Internet Explorer")!=-1 && document.all;  
    3. }  
    4. function isIE6() {  
    5.     return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 6.0")=="-1"?false:true;  
    6. }  
    7. function isIE7(){  
    8.     return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 7.0")=="-1"?false:true;  
    9. }  
    10. function isIE8(){  
    11.     return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 8.0")=="-1"?false:true;  
    12. }  
    13. function isNN(){  
    14.     return navigator.userAgent.indexOf("Netscape")!=-1;  
    15. }  
    16. function isOpera(){  
    17.     return navigator.appName.indexOf("Opera")!=-1;  
    18. }  
    19. function isFF(){  
    20.     return navigator.userAgent.indexOf("Firefox")!=-1;  
    21. }  
    22. function isChrome(){  
    23.     return navigator.userAgent.indexOf("Chrome") > -1;    
    24. }  

     

    2,第二种称为 对象/特征 检测方式,这是一种判断浏览器能力的方式,也是目前流行的方式。即在使用一个对象之前检测它是否存在。上面提到的addEvent方法中就使用了该方式。.addEventListener是w3c dom标准方式,而IE使用自己特有attachEvent。以下列举几个:

    a,talbe.cells只有IE/Opera支持。

    b,innerText/insertAdjacentHTML除Firefox外,IE6/7/8/Safari/Chrome/Opera都支持。

    c,window.external.AddFavorite用来在IE下添加到收藏夹。

    d,window.sidebar.addPanel用来在FF下添加到收藏夹。

    3,第三种很有趣,暂且称为 浏览器缺陷或bug 方式,即某些表现不是浏览器厂商刻意实现的。如下:

    Js代码  收藏代码
    1. var isIE = !+"\v1";  
    2. var isIE = !-[1,];  
    3. var isIE = "\v"=="v";  
    4. isSafari=/a/.__proto__=='//';  
    5. isOpera=!!window.opera;  

     

    最经典的莫过于 !-[1,] 的判断方式,目前最少代码判断IE的方式,只需6个byte。这是个俄国人 发现的。利用了数组[1,]的length。还有来自英国的年轻 James Padolsey 利用IE条件注释

    Js代码  收藏代码
    1. var ie = (function(){  
    2.     var undef,  
    3.         v = 3,  
    4.         div = document.createElement('div'),  
    5.         all = div.getElementsByTagName('i');  
    6.    
    7.     while (  
    8.         div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',  
    9.         all[0]  
    10.     );   
    11.     return v > 4 ? v : undef  
    12. }());  

    被称为史上最有创意的IE判断。

    注1:isIE = "\v" == "v" 方式IE9已经修复该bug,不能用此方式判断IE浏览器了(2010-6-29用IE9 pre3测试的)

    知识共享许可协议
    作品Tim Zhang创作,采用知识共享署名 3.0 中国大陆许可协议进行许可。 。
  • 相关阅读:
    .NET基础篇——利用泛型与反射更新实体(ADO.NET Entity Framework)(转)
    反射奥秘
    ubuntu 安装chmsee
    ubuntu安装sublime,新立得
    Ubuntu 升级VisualBox后无法启动 Kernel driver not installed
    ubuntu安装stardict并导入词典
    PHP运行模式
    Ubuntu ibus输入法图标消失的解决办法
    MySQL 获得当前日期时间(以及时间的转换)
    mysql索引之or条件
  • 原文地址:https://www.cnblogs.com/ccdc/p/2647056.html
Copyright © 2020-2023  润新知