码迷,mamicode.com
首页 > 其他好文 > 详细

浏览器嗅探器(转)

时间:2019-06-22 18:18:24      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:ini   use   row   red   win   end   bst   int   identity   

浏览器检测是在工作中经常用到的,如果只是简单判断当前是什么浏览器的话可以通过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. }
  18. else if (dataProp)
  19. return data[i].identity;
  20. }
  21. },
  22. searchVersion: function (dataString) {
  23. var index = dataString.indexOf(this.versionSearchString);
  24. if (index == -1) return;
  25. return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  26. },
  27. dataBrowser: [
  28. {
  29. string: navigator.userAgent,
  30. subString: "Chrome",
  31. identity: "Chrome"
  32. },
  33. { string: navigator.userAgent,
  34. subString: "OmniWeb",
  35. versionSearch: "OmniWeb/",
  36. identity: "OmniWeb"
  37. },
  38. {
  39. string: navigator.vendor,
  40. subString: "Apple",
  41. identity: "Safari",
  42. versionSearch: "Version"
  43. },
  44. {
  45. prop: window.opera,
  46. identity: "Opera"
  47. },
  48. {
  49. string: navigator.vendor,
  50. subString: "iCab",
  51. identity: "iCab"
  52. },
  53. {
  54. string: navigator.vendor,
  55. subString: "KDE",
  56. identity: "Konqueror"
  57. },
  58. {
  59. string: navigator.userAgent,
  60. subString: "Firefox",
  61. identity: "Firefox"
  62. },
  63. {
  64. string: navigator.vendor,
  65. subString: "Camino",
  66. identity: "Camino"
  67. },
  68. { // for newer Netscapes (6+)
  69. string: navigator.userAgent,
  70. subString: "Netscape",
  71. identity: "Netscape"
  72. },
  73. {
  74. string: navigator.userAgent,
  75. subString: "MSIE",
  76. identity: "Internet Explorer",
  77. versionSearch: "MSIE"
  78. },
  79. {
  80. string: navigator.userAgent,
  81. subString: "Gecko",
  82. identity: "Mozilla",
  83. versionSearch: "rv"
  84. },
  85. { // for older Netscapes (4-)
  86. string: navigator.userAgent,
  87. subString: "Mozilla",
  88. identity: "Netscape",
  89. versionSearch: "Mozilla"
  90. }
  91. ],
  92. dataOS : [
  93. {
  94. string: navigator.platform,
  95. subString: "Win",
  96. identity: "Windows"
  97. },
  98. {
  99. string: navigator.platform,
  100. subString: "Mac",
  101. identity: "Mac"
  102. },
  103. {
  104. string: navigator.userAgent,
  105. subString: "iPhone",
  106. identity: "iPhone/iPod"
  107. },
  108. {
  109. string: navigator.platform,
  110. subString: "Linux",
  111. identity: "Linux"
  112. }
  113. ]
  114. };
  115. BrowserDetect.init();

使用方式如下:

技术图片

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

浏览器嗅探器(转)

标签:ini   use   row   red   win   end   bst   int   identity   

原文地址:https://www.cnblogs.com/hyds/p/11069790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!