浏览器嗅探器(转)

Posted hyds

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浏览器嗅探器(转)相关的知识,希望对你有一定的参考价值。

浏览器检测是在工作中经常用到的,如果只是简单判断当前是什么浏览器的话可以通过window.navigator.useragent这样的js来直接判断就可以了!

但是针对浏览器版本要求比较高的时候,如果只是单纯的判断浏览器的名字的话还不能解决问题,这时候就要通过其他方式来实现!现有的jquery已经废弃了$.browser的使用,所以在这里我介绍一个叫做"浏览器嗅探器"的程序来实现!

代码如下:(BrowserDetect.js)

  1. var BrowserDetect =
  2. init: function ()
  3. this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
  4. this.version = this.searchVersion(navigator.userAgent)
  5. || this.searchVersion(navigator.appVersion)
  6. || "an unknown version";
  7. this.OS = this.searchString(this.dataOS) || "an unknown OS";
  8. ,
  9. searchString: function (data)
  10. for (var i=0;i<data.length;i++)
  11. var dataString = data[i].string;
  12. var dataProp = data[i].prop;
  13. this.versionSearchString = data[i].versionSearch || data[i].identity;
  14. if (dataString)
  15. if (dataString.indexOf(data[i].subString) != -1)
  16. return data[i].identity;
  17. else if (dataProp)
  18. return data[i].identity;
  19. ,
  20. searchVersion: function (dataString)
  21. var index = dataString.indexOf(this.versionSearchString);
  22. if (index == -1) return;
  23. return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  24. ,
  25. dataBrowser: [
  26. string: navigator.userAgent,
  27. subString: "Chrome",
  28. identity: "Chrome"
  29. ,
  30. string: navigator.userAgent,
  31. subString: "OmniWeb",
  32. versionSearch: "OmniWeb/",
  33. identity: "OmniWeb"
  34. ,
  35. string: navigator.vendor,
  36. subString: "Apple",
  37. identity: "Safari",
  38. versionSearch: "Version"
  39. ,
  40. prop: window.opera,
  41. identity: "Opera"
  42. ,
  43. string: navigator.vendor,
  44. subString: "iCab",
  45. identity: "iCab"
  46. ,
  47. string: navigator.vendor,
  48. subString: "KDE",
  49. identity: "Konqueror"
  50. ,
  51. string: navigator.userAgent,
  52. subString: "Firefox",
  53. identity: "Firefox"
  54. ,
  55. string: navigator.vendor,
  56. subString: "Camino",
  57. identity: "Camino"
  58. ,
  59. // for newer Netscapes (6+)
  60. string: navigator.userAgent,
  61. subString: "Netscape",
  62. identity: "Netscape"
  63. ,
  64. string: navigator.userAgent,
  65. subString: "MSIE",
  66. identity: "Internet Explorer",
  67. versionSearch: "MSIE"
  68. ,
  69. string: navigator.userAgent,
  70. subString: "Gecko",
  71. identity: "Mozilla",
  72. versionSearch: "rv"
  73. ,
  74. // for older Netscapes (4-)
  75. string: navigator.userAgent,
  76. subString: "Mozilla",
  77. identity: "Netscape",
  78. versionSearch: "Mozilla"
  79. ],
  80. dataOS : [
  81. string: navigator.platform,
  82. subString: "Win",
  83. identity: "Windows"
  84. ,
  85. string: navigator.platform,
  86. subString: "Mac",
  87. identity: "Mac"
  88. ,
  89. string: navigator.userAgent,
  90. subString: "iPhone",
  91. identity: "iPhone/iPod"
  92. ,
  93. string: navigator.platform,
  94. subString: "Linux",
  95. identity: "Linux"
  96. ]
  97. ;
  98. BrowserDetect.init();

使用方式如下:

技术图片

 这个js兼容非常不错!使用起来比较简单,而且功能强大! 

以上是关于浏览器嗅探器(转)的主要内容,如果未能解决你的问题,请参考以下文章

WebSiteSniffer(网站资源嗅探器)

网络嗅探是啥?

WebCookiesSniffer(网页Cookies嗅探器)

WhoIsConnectedSniffer(网络连接嗅探器)

带有嗅探器代理的 Adob​​e Air?

浏览器嗅探是啥意思?