根据特征的浏览器判断

今天在csdn上看到一个很久以前用的浏览器判断,大多数都通不过了,现在修改了下,我本机上几个浏览器都测试通过
var Sys = {},
ua 
= navigator.userAgent;
if (window.ActiveXObject)
    Sys.ie 
= ua.match(/MSIE ([\d.]+)/)[1]
else if (window.google && window.chrome){
    Sys.chrome 
= ua.match(/Chrome\/([\d.]+)/)[1]}
else if (window.Components)
    Sys.firefox 
= ua.match(/Firefox\/([\d.]+)/)[1]
else if (window.opera)
    Sys.opera 
= ua.match(/Opera.([\d.]+)/)[1]
else if (!navigator.taintEnabled) 
    Sys.safari 
= ua.match(/Version\/([\d.]+)/)[1];

//以下进行测试
if(Sys.ie) document.write('IE: '+Sys.ie);
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
if(Sys.opera) document.write('Opera: '+Sys.opera);
if(Sys.safari) document.write('Safari: '+Sys.safari);

 

又整理了一个,特性检测:

var browser=function(){//尽量使用特性去检测
    var ua=navigator.userAgent,
    b 
= {
        ie : 
!!window.ActiveXObject,
        
//!+'\v1'
        //!!window.VBArray
        ie6 : !"1"[0],
        ie67 : 
/MSIE [67]/.test(ua),
        ie9 : 
!!document.documentMode && document.documentMode===9,
        chrome : 
!!(window.google && window.chrome),
        firefox : 
!!window.Components,
        safari : 
!!navigator.vendor && !top.chrome,//(!navigator.taintEnabled && !window.chrome),  奇怪的IE6/7报错,
        opera : !!window.opera,
        version : (ua.match(
/\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
    };    
    
return b
}()
//显示结果
document.write("<br/><br/>"+navigator.userAgent+"<br/>")
var c=browser;
for(var n in c){
    document.write(n
+" : "+c[n]+"<br/>")
}
 

 

ua检测:

var bb=function(){
    
var ua=navigator.userAgent,
    check
=function(r){return r.test(ua)},
    b 
= {
        ie : check(
/MSIE/),
        ie6 : check(
/MSIE 6/),
        ie67 : check(
/MSIE [67]/),
        ie9 : check(
/MSIE 9/),
        chrome : check(
/Chrome/),
        firefox : check(
/Firefox/),
        safari : check(
/Safari/&& !top.chrome,
        opera : check(
/Opera/),
        version : (ua.match(
/\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
    };    
    
return b
}()

 

 

相关链接:

javascript浏览器判断

Browser detect

 

 

posted @ 2010-06-14 02:00  sohighthesky  阅读(681)  评论(3编辑  收藏  举报