Js 判斷瀏覽器類型

來源:互聯網
上載者:User

今天,還有個備受打擊的問題,怎麼通過js來判定瀏覽器版本。初出茅廬時,我曾嘗試自個寫方法判定,後來jquery庫之類的大量使用,已經忘記這個事了,記得以前看ibw代碼時,發現了段相關代碼,不過一直在草稿箱中,沒有公布。這裡也show,show吧,爭取下次不要再犯同類錯誤:


[javascript]
_t.Browser = function ( sUA ){// IBW.Tool.Browser  
var win = window , 
c = function (r){ 
return r.test(sUA); 
}, 
isIE = !+'\v1', 
isTrident = isIE || !!win.XDomainRequest, 
isWebkit = !isTrident &&  !navigator.taintEnabled; 
return { 
//瀏覽器嗅探   
isIE  : isIE, 
isIE6 : isIE && (!win.XMLHttpRequest || !!win.XMLHttpRequest.constructor), 
isIE7 : isIE && !!win.XMLHttpRequest && !win.XMLHttpRequest.constructor && !win.XDomainRequest, 
isIE8 : isIE && !!win.XDomainRequest, 
isIE9 : !isIE && !!win.XDomainRequest,  
isMaxthon :!win.opera && !!win.external && typeof external.max_version !== 'undefined' , //opera 讀external會拋出異常. external為 null  
isTT : c(/; TencentTraveler/), 
is360 : c(/; 360/) , 
isSogou : c(/; SE \d\./), 
//渲染引擎嗅探.  
isWebkit : isWebkit ,//safari chrome KHTML 渲染引擎.  !!+'\v1' 防止ie 6 報錯  
isGecko : !!document.getBoxObjectFor || win.mozInnerScreenX != null,//firefox 渲染引擎  
isTrident : isTrident, //ie渲染引擎  
isPresto : !!win.opera,//opera 渲染引擎  
 
//擴充嗅探  
 
//判斷是否為高速模式的sougou瀏覽器  
isFastModeSogou : isWebkit && !!win.external && win.external.propertyIsEnumerable('StartPageCall'), 
 
//判斷是否為高速模式的sougou瀏覽器 並且需要在修複flash模式中 觸發reflow  (sougou2.0 fastMode)  
isFastModeSogouNeedReflow : isWebkit && !!win.external && win.external.propertyIsEnumerable('StartPageCall') && !('localdb' in external), 
 
//判斷是否為高速模式的QQ5瀏覽器  
isFastModeQQ : isWebkit && win.external && 'WebCorVer' in external, 
 
//判斷是否支援iframe 修複flash遮擋div的bug  
isIframeOnFlash : isTrident || function(){for(var o in {1:1,0:1}) return o =='0'; return '_'}() || (win.mozInnerScreenX == null && !!document.getBoxObjectFor) 
}; 
}(win.navigator.userAgent) ; 

_t.Browser = function ( sUA ){// IBW.Tool.Browser
var win = window ,
c = function (r){
return r.test(sUA);
},
isIE = !+'\v1',
isTrident = isIE || !!win.XDomainRequest,
isWebkit = !isTrident &&  !navigator.taintEnabled;
return {
//瀏覽器嗅探
isIE  : isIE,
isIE6 : isIE && (!win.XMLHttpRequest || !!win.XMLHttpRequest.constructor),
isIE7 : isIE && !!win.XMLHttpRequest && !win.XMLHttpRequest.constructor && !win.XDomainRequest,
isIE8 : isIE && !!win.XDomainRequest,
isIE9 : !isIE && !!win.XDomainRequest,
isMaxthon :!win.opera && !!win.external && typeof external.max_version !== 'undefined' , //opera 讀external會拋出異常. external為 null
isTT : c(/; TencentTraveler/),
is360 : c(/; 360/) ,
isSogou : c(/; SE \d\./),
//渲染引擎嗅探.
isWebkit : isWebkit ,//safari chrome KHTML 渲染引擎.  !!+'\v1' 防止ie 6 報錯
isGecko : !!document.getBoxObjectFor || win.mozInnerScreenX != null,//firefox 渲染引擎
isTrident : isTrident, //ie渲染引擎
isPresto : !!win.opera,//opera 渲染引擎

//擴充嗅探

//判斷是否為高速模式的sougou瀏覽器
isFastModeSogou : isWebkit && !!win.external && win.external.propertyIsEnumerable('StartPageCall'),

//判斷是否為高速模式的sougou瀏覽器 並且需要在修複flash模式中 觸發reflow  (sougou2.0 fastMode)
isFastModeSogouNeedReflow : isWebkit && !!win.external && win.external.propertyIsEnumerable('StartPageCall') && !('localdb' in external),

//判斷是否為高速模式的QQ5瀏覽器
isFastModeQQ : isWebkit && win.external && 'WebCorVer' in external,

//判斷是否支援iframe 修複flash遮擋div的bug
isIframeOnFlash : isTrident || function(){for(var o in {1:1,0:1}) return o =='0'; return '_'}() || (win.mozInnerScreenX == null && !!document.getBoxObjectFor)
};
}(win.navigator.userAgent) ;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.