判斷一下瀏覽器類型(JS判斷IE,FF等)

判斷一下瀏覽器類型,然後設置寬度啊就好了
下面是判斷瀏覽器的代碼,你用DOM設置寬度就好了
第一種,只區分瀏覽器,不考慮版本

<!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 userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判斷是否Opera瀏覽器
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 ; //判斷是否Safari瀏覽器


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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章