判断safari_判断IE_判断firefox_判断Opera—JS判断浏览器

By柏小白

判断safari_判断IE_判断firefox_判断Opera—JS判断浏览器

JS

//----------------------------- 判断浏览器 -------------------------  
function myBrowser(){  
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera  
var isMaxthon = userAgent.indexOf("Maxthon") > -1 ; //判断是否傲游3.0  
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ; //判断是否IE   
var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox  
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") < 1 ; //判断是否Safari  
var isChrome = userAgent.indexOf("Chrome") > -1 ; //判断是否Chrome  
  
if(isIE){   
   var IE5 = IE55 = IE6 = IE7 = IE8 = false;  
   var reIE = new RegExp("MSIE (+);");  
   reIE.test(userAgent);  
   var fIEVersion = parseFloat(RegExp["$1"]);  
  
   IE55 = fIEVersion == 5.5 ;  
   IE6 = fIEVersion == 6.0 ;  
   IE7 = fIEVersion == 7.0 ;  
   IE8 = fIEVersion == 8.0 ;  
  
   if(IE55){ return "IE55"; }  
   if(IE6){ return "IE6"; }  
   if(IE7){ return "IE7"; }  
   if(IE8){ return "IE8"; }  
}  
  
if(isFF){ return "FF"; }  
if(isOpera){ return "Opera"; }  
if(isMaxthon){ return "Maxthon"; }  
if(isSafari){ return "Safari"; }  
if(isChrome){ return "Chrome"; }  
  
} //myBrowser() end  
  
  
window.onload=function(){  
  
document.getElementById("ua").innerHTML=navigator.userAgent;  
  
if(myBrowser()=="FF"){alert("我是 Firefox");}  
if(myBrowser()=="Maxthon"){alert("我是傲游(webkit核心)");}  
if(myBrowser()=="Opera"){alert("我是 Opera");}  
if(myBrowser()=="Safari"){alert("我是 Safari");}  
if(myBrowser()=="Chrome"){alert("我是 Chrome");}  
if(myBrowser().indexOf("IE")>-1){alert("我是 IE");}   
if(myBrowser()=="IE55"){alert("我是 IE5.5");}  
if(myBrowser()=="IE6"){alert("我是 IE6");}  
if(myBrowser()=="IE7"){alert("我是 IE7");}  
if(myBrowser()=="IE8"){alert("我是 IE8");}  
}

Jquery判断浏览器版本:

$.browser.msie; 
if ($.browser.webkit) { alert( "this is webkit!" ); }
if ($.browser.safari) { alert( "this is safari" ); }
var ua = $.browser; if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) { alert( "Do stuff for firefox 3" ); }
if ( $.browser.msie ) { $("#div ul li").css( "display","inline" ); } else { $("#div ul li").css( "display","inline-table" ); }

 

About the author

柏小白 administrator