A-A+

JS判断浏览器类型的方法总结

2016年01月28日 前端设计 暂无评论 阅读 6 views 次

JS中判断浏览器的 类型,估计是每个编辑过页面的开发人员都遇到过的问题。在众多的浏览器产品中,IE、Firefox、Opera、Safari……..众多品牌 却标准不一,因此时常需要根据不同的浏览器,甚至相同浏览器不同版本做不同的操作,因此,知晓浏览器的判断方法,还是很重要的。

各类浏览器在JS的navigator.userAgent.toLowerCase()中显示结果.

1 火狐 mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.0.10) gecko/2009042316 firefox/3.0.10 qqdownload/1.7

2 IE8 mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; .net clr 2.0.50727; .net clr 3.0.4506.2152; .net clr 3.5.30729)

3 谷歌 mozilla/5.0 (windows; u; windows nt 5.1; en-us) applewebkit/532.0 (khtml, like gecko) chrome/4.0.211.2 safari/532.0

4 Opera opera/9.80 (windows nt 5.1; u; zh-cn) presto/2.6.30 version/10.61

5 苹果 mozilla/5.0 (windows; u; windows nt 5.1; zh-cn) applewebkit/533.16 (khtml, like gecko) version/5.0 safari/533.16

6 IE6 mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)

下面列举一下常用的判断方法,代码如下:

  1. <script type="text/javascript">  
  2.         var Sys = {};  
  3.         var ua = navigator.userAgent.toLowerCase();  
  4.         var s;  
  5.         (s = ua.match(/msie ([d.]+)/)) ? Sys.ie = s[1] :  
  6.         (s = ua.match(/firefox/([d.]+)/)) ? Sys.firefox = s[1] :  
  7.         (s = ua.match(/chrome/([d.]+)/)) ? Sys.chrome = s[1] :  
  8.         (s = ua.match(/opera.([d.]+)/)) ? Sys.opera = s[1] :  
  9.         (s = ua.match(/version/([d.]+).*safari/)) ? Sys.safari = s[1] : 0;  
  10.         //以下进行测试  
  11.         if (Sys.ie) document.write('IE: ' + Sys.ie);  
  12.         if (Sys.firefox) document.write('Firefox: ' + Sys.firefox);  
  13.         if (Sys.chrome) document.write('Chrome: ' + Sys.chrome);  
  14.         if (Sys.opera) document.write('Opera: ' + Sys.opera);  
  15.         if (Sys.safari) document.write('Safari: ' + Sys.safari);  
  16.     </script>  

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代码如下:

  1. <script language="JavaScript">  
  2. <!–  
  3. function getOs()  
  4. {  
  5. var OsObject = "";  
  6. if(navigator.userAgent.indexOf("MSIE")>0) {  
  7. return "MSIE";  
  8. }  
  9. if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){  
  10. return "Firefox";  
  11. }  
  12. if(isSafari=navigator.userAgent.indexOf("Safari")>0) {  
  13. return "Safari";  
  14. }  
  15. if(isCamino=navigator.userAgent.indexOf("Camino")>0){  
  16. return "Camino";  
  17. }  
  18. if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){  
  19. return "Gecko";  
  20. }  
  21. }  
  22. alert("您的浏览器类型为:"+getOs());  
  23. –>  
  24. </script>  
标签:

给我留言