JS判斷瀏覽器類型與版本

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   java   for   sp   

在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判斷瀏覽器類型與版本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.