iLeichun

当前位置: 首页 > 网站建设

JavaScript判断浏览器类型与版本

分类:网站建设   来源:网络   时间:2011-11-10 00:09:12

/**
* @deprecated 获取浏览器类型,调用getOS();
* @return 浏览器名字
*
*/
function getOS()
{
if(navigator.userAgent.indexOf("MSIE")>0)
{
   return "MSIE";
}
else if(isFirefox = navigator.userAgent.indexOf("Firefox")>0)
{
   return "Firefox";
}
else if(isSafari = navigator.userAgent.indexOf("Safari")>0)
{
   return "Safari";
}
else if(isCamino = navigator.userAgent.indexOf("Camino")>0)
{
   return "Camino";
}
else if(isMozilla = navigator.userAgent.indexOf("Gecko/")>0)
{
   return "Gecko";
}
else
{
   return "";
}
}

function versionNumber()
{
return parseFloat(navigator.appVersion);
}

var isFirefox2 = navigator.userAgent.indexOf("Firefox")>0 && parseFloat(navigator.appVersion) == 2;
var isFirefox3 = navigator.userAgent.indexOf("Firefox")>0 && parseFloat(navigator.appVersion) == 3;

var isIE6 = navigator.userAgent.indexOf("MSIE")>0 && parseFloat(navigator.appVersion) == 6;
var isIE7 = navigator.userAgent.indexOf("MSIE")>0 && parseFloat(navigator.appVersion) == 7;
第一种,只区分浏览器,不考虑版本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> 判断浏览器</title>

<script type="text/javascript">

function myBrowser(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1;

if (isOpera){return "Opera"}; //判断是否Opera浏览器  
if (userAgent.indexOf("Firefox") > -1){return "FF";} //判断是否Firefox
if (userAgent.indexOf("Safari") > -1){return "Safari";} //判断是否Safari
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera){return "IE";} ; //判断是否IE
} //myBrowser() end

//调用上面的函数

if(myBrowser()=="IE"){alert("IE");}
if(myBrowser()=="FF") {alert("Firefox");}
if(myBrowser()=="Opera"){alert("Opera");}
if(myBrowser()=="Safari") {alert("Safari");}

//下面这两句是显示 navigator.userAgent 信息 以方便理解
var userAgent = navigator.userAgent;
document.write(userAgent);
</script>
</head>

<body>
</body>
</html>

第二种,区分浏览器,并考虑IE5.5 6 7 8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> 判断浏览器</title>
<script type="text/javascript">
function myBrowser(){
var isFF = userAgent.indexOf("Firefox") > -1 ; //判断是否Firefox
var isSafari = userAgent.indexOf("Safari") > -1 ; //判断是否Safari

var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera ; //判断是否IE 
 

if(isIE){
   var IE5 = IE55 = IE6 = IE7 = IE8 = false;
   var reIE = new RegExp("MSIE (d+.d+);");
   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"; }
}//isIE end

if(isFF){ return "FF"; }
if(isOpera){ return "Opera"; }

}//myBrowser() end

//调用上面的函数

if(myBrowser()=="FF") {alert("Firefox");}
if(myBrowser()=="Opera"){alert("Opera");}
if(myBrowser()=="Safari") {alert("Safari");}

if(myBrowser()=="IE55"){alert("IE5.5");}
if(myBrowser()=="IE6") {alert("IE6");}
if(myBrowser()=="IE7"){alert("IE7");}
if(myBrowser()=="IE8") {alert("IE8");}

//下面这两句是显示 navigator.userAgent 信息 以方便理解
var userAgent = navigator.userAgent;
document.write(userAgent);
</script>
</head>

<body >
</body>
</html>

更多