判断浏览器兼容

来源:互联网 发布:斗破苍穹网络剧在哪播 编辑:程序博客网 时间:2024/05/15 16:39


UserAgent是浏览器用于 HTTP 请求的用户代理头的值

window.close()方法在Chrome浏览器中不好用。

先是使用navigator.appName方法。

但是IE之外的浏览器(Safari,FireFox,Chrome)显示的都是Netscape

这种方法,无法单独区分Chrome浏览器。

解决:

使用navigator.userAgent;

看看在这里面能否找到Chrome关键字。

具体使用的代码:

if(navigator.userAgent.indexOf("Chrome") == -1){   

    window.close();    

}else{  

    // 可以找到Chrome,表明是Chrome浏览器。  

   window.open('','_self','');   

    window.close();  




//判断是否是ie,因为现在的ie11没有msie,所以判断ie不能用document.all,ie10下的都是1,其他浏览器都是0
       if(!!window .ActiveXObject || "ActiveXObject" in window){
             var ie6 = navigator.userAgent .indexOf ("MSIE 6.0")>0 ;
             var ie7 = navigator.userAgent .indexOf ("MSIE 7.0")>0 ;
             var ie8 = navigator.userAgent .indexOf ("MSIE 8.0")>0 ;
             var ie9 = navigator.userAgent .indexOf ("MSIE 9.0")>0 ;
             var ie5 = navigator.userAgent .indexOf ("MSIE 5.0")>0 ;
             //ie9及以下不调用动态显示,不支持websocket
             if(!ie6 + !ie7 + !ie8 + !ie9+ !ie5 == 5){
                  webSocketPolling ();
             }
       }





0 0