js判断客户浏览器类型,版本

在JS中判断浏览器的 类型,估计是每一个编辑过页面的开发人员都遇到过的问题。在众多的浏览器产品中,IE、Firefox、Opera、Safari........众多品牌 却标准不一,所以时常须要根据不一样的浏览器,甚至相同浏览器不一样版本作不一样的操做,所以,知晓浏览器的判断方法,仍是很重要的。下面列举一下经常使用的判断方 法:
一、判断浏览器是否为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描述浏览器名称信息。
二、判断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':同上。
三、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> java

相关文章
相关标签/搜索