javascript判定不同浏览器

时间:2022-10-21 15:38:06

除了另无它法,肯定不使用navigator.userAgent来判定浏览器。因为在第一次浏览器大战初期,Netscape占绝对统计地位,大 部分人们不愿意兼容其他浏览器,并通过检测其UA让他们的网站只允许Netscape访问,这就逼使其他浏览器(包括IE)修改自己的UA伪装成 Netscape来通过那些自以为是的脚本,于是出现每个人都声称自己是别人的局面,即使最新的IE9的UA也是:

 

 //Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)

// 2010 4 16 日更新
         ie = !+ "\v1" ;
         ie = !-[ 1 ,];//IE 9 预览版中失效
         ie = '\v' == 'v' ;
         ie = !!document.recalc
         ie = !!window.VBArray
         ie = !!window.ActiveXObject
         ie = 0 //@cc_on+ 1 ;
         ie = !!window.createPopup;
         ie = /*@cc_on!@*/ ! 1 ;
         ie = document.expando;//document. all 在opera firefox的古老版本也存在
         ie = /\w/.test( '\u0130' ) //由群里的abcd友情提供
        
         ie 6 = ! "1" [ 0 ] //利用IE 6 或IE 5 的字符串不能使用数组下标的特征
         ie 8 = !!window.XDomainRequest;
         ie 9 =  document.documentMode && document.documentMode === 9 ;
         //自创,基于条件编译的嗅探脚本,IE会返回其JS引擎的版本号,非IE返回 0
         var ieVersion = eval( "''+/*@cc_on" + " @_jscript_version@*/-0" )* 1
         ie 9 = ieVersion === 5.9
         ie 8 = ieVersion === 5.8
         ie 7 = ieVersion === 5.7
         ie 6 = ieVersion === 5.6
         ie 5 = ieVersion === 5.5
         netscape = !!window.GeckoActiveXObject
         gecko  = !!window.netscape //包括firefox
         firefox = !!window.Components
         firefox = !!window.updateCommands
         safari = !!(navigator.vendor && navigator.vendor.match(/Apple/))
         safari = window.openDatabase && !window.chrome;
         chrome= !!(window.chrome && window.google)
         opera=!!window.opera ;
         //傲游 2 3
         maxthon = /maxthon/i.test(navigator.userAgent)
         // 360 安全浏览器
         is 360 se = / 360 se/i.test(navigator.userAgent)
// 2010.6 . 4
        setTimeout(function(){//FF的setTimeout总是有一个额余的参数 0
           var isFF = !!arguments.length;
           alert(isFF)
         }, 0 );
 
//判定IE版本
2010.10 . 1
       ie = (function(undefined){
         var v = 3 , div = document.createElement( 'div' );
         while (
         div.innerHTML = '<!--[if gt IE ' +(++v)+ ']><i></i><![endif]-->' ,
         div.getElementsByTagName( 'i' )[ 0 ]);
         return v> 4 ? v : undefined;
       }());
 
//判定IE版本 2011.2 . 24
        ie = (function() {
           var v = 3 , div = document.createElement( 'div' ), a = div. all || [];
           while (div.innerHTML = '<!--[if gt IE ' +(++v)+ ']><br><![endif]-->' , a[ 0 ]);
           return v > 4 ? v : !v;
         }());
 
 
 
 
//手机的相关判定 2011.9 . 21
  isIPhone = /iPhone/i.test(navigator.userAgent);
  isIPhone 4 = window.devicePixelRatio >= 2
//在网页中,pixel与point比值称为device-pixel-ratio,普通设备都是 1 ,iPhone 4 2 ,有些Android机型是 1.5
//http://blog.webcreativepark.net/ 2011 / 01 / 25 -173502 .html
  isIPad = /iPad/i.test(navigator.userAgent);
  isAndroid = /android/i.test(navigator.userAgent);
  isIOS = isIPhone  || isIPad ;
 
 
 
 
 

isMobile = isIOS || isAndroid ;