星空网 > 软件开发 > Java

js中浏览器类型判断

在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中浏览器类型判断

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

云南东川红土地旅游线路是什么?:https://www.vstour.cn/a/396225.html
从长沙火车站到省博物馆,橘子洲,坡子街怎么做公交:https://www.vstour.cn/a/396226.html
昆明中国国旅(打造一次完美的旅行体验):https://www.vstour.cn/a/396227.html
茂林风景区有什么景点:https://www.vstour.cn/a/396228.html
罗源湾海洋世界8个景点图 罗源湾海洋世界旅游攻略:https://www.vstour.cn/a/396229.html
枫泾古镇门票 枫泾古镇门票团购:https://www.vstour.cn/a/396230.html
重磅利好!欧洲大国暂停100多种商品进口税:https://www.kjdsnews.com/a/1842170.html
申根签证页编号是什么意思 申根签证内容:https://www.vstour.cn/a/409227.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流