JS判斷瀏覽器是否為IE 以及螢幕解析度

saucej發表於2014-11-28
<script type="text/javascript"> 
var dstURL  = "http://www.baidu.com"
if(-[1,]){ 
//alert("this is not ie"); 
//window.location.href = "http://www.baidu.com"
}else{ 
//alert("this is ie");
   var width = window.screen.width;
    if(width>1024)
    {
   //     alert("clientscreen bigger than 1024");
      window.location.href = dstURL;
    }
    else
    {
  //      alert("smaller than 1024");
    }
} 
</script>


瀏覽器版本判斷

在JS中判斷瀏覽器的型別,估計是每個編輯過頁面的開發人員都遇到過的問題。在眾多的瀏覽器產品中,IE、Firefox、Opera、Safari........眾多品牌卻標準不一,因此時常需要根據不同的瀏覽器,甚至相同瀏覽器不同版本做不同的操作,因此,知曉瀏覽器的判斷方法,還是很重要的。下面列舉一下常用的判斷方法:
1、判斷瀏覽器是否為IE
document.all ? 'IE' : 'others':在IE下document.all值為1,而其他瀏覽器下的值為0;
navigator.userAgent.indexOf("MSIE")>0 ? 'IE' : 'others':navigator.userAgent是描述使用者代理資訊。
navigator.appName.indexOf("Microsoft") != -1 ? 'IE' : 'others':navigator.appName描述瀏覽器名稱資訊。
2、判斷IE版本
navigator.appVersion.match(/6./i)=="6." ? 'IE6' : 'other version':在已知是IE瀏覽器的情況下,可以通過此方法判斷是否是IE6;
navigator.userAgent.indexOf("MSIE 6.0")>0 ? 'IE7' : 'other version':同上;
navigator.appVersion.match(/7./i)=="7." ? 'IE7' : 'other version':在已知是IE瀏覽器的情況下,可以通過此方法判斷是否是IE7;
navigator.userAgent.indexOf("MSIE 7.0")>0 ? 'IE7' : 'other version':同上;
navigator.appVersion.match(/8./i)=="8." ? 'IE8' : 'other version':在已知是IE瀏覽器的情況下,可以通過此方法判斷是否是IE8;
navigator.userAgent.indexOf("MSIE 8.0")>0 ? 'IE8' : 'other version':同上。
3、JS獲取瀏覽器資訊
瀏覽器程式碼名稱:navigator.appCodeName
瀏覽器名稱:navigator.appName
瀏覽器版本號:navigator.appVersion
Java的支援:navigator.javaEnabled()
MIME型別(陣列):navigator.mimeTypes
系統平臺:navigator.platform
外掛(陣列):navigator.plugins
使用者代理:navigator.userAgent

DEMO:
Js程式碼
<script language="JavaScript">
<!--
function getOs()
{
var OsObject = "";
if(navigator.userAgent.indexOf("MSIE")>0) {
return "MSIE";
}
if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
return "Firefox";
}
if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
return "Safari";
}
if(isCamino=navigator.userAgent.indexOf("Camino")>0){
return "Camino";
}
if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
return "Gecko";
}

}
alert("您的瀏覽器型別為:"+getOs());
-->
</script>

JS螢幕解析度屬性

網頁可見區域寬:document.body.clientwidth 
網頁可見區域高:document.body.clientheight 
網頁可見區域寬:document.body.offsetwidth (包括邊線和滾動條的寬) 
網頁可見區域高:document.body.offsetheight(包括邊線的寬) 
網頁正文全文寬:document.body.scrollwidth 
網頁正文全文高:document.body.scrollheight 
網頁被捲去的高:document.body.scrolltop 
網頁被捲去的左:document.body.scrollleft 
網頁正文部分上:window.screentop 
網頁正文部分左:window.screenleft 
螢幕解析度的高:window.screen.height 
螢幕解析度的寬:window.screen.width 
螢幕可用工作區高度:window.screen.availheight 
螢幕可用工作區寬度:window.screen.availwidth 
螢幕設定 window.screen.colordepth 位彩色 
螢幕設定 window.screen.devicexdpi 畫素/英寸
本文原始連結:<a target=_blank href="http://www.jbxue.com/article/17848.html">http://www.jbxue.com/article/17848.html</a>

 

 

 

 

相關文章