用户代理字符串

safari: 2003年,Apple公司发布自己的Web浏览器,名字为Safari。safari的呈现引擎叫WebKit,是Linux平台中Konqueror浏览器的呈现引擎KHTML的一个分支。几年后,WebKit独立出来专注于呈现引擎的开发
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6

chrome: 谷歌公司的Chrome浏览器以WebKit作为呈现引擎,但使用了不同的JS引擎
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

识别代码

  1. var client = function() {
  2. // 呈现引擎
  3. var engine = {
  4. ie: 0,
  5. gecko: 0,
  6. webkit: 0,
  7. khtml: 0,
  8. opera: 0,
  9. // 完整的版本号
  10. ver: null
  11. };
  12. // 浏览器
  13. var browser = {
  14. ie: 0,
  15. firefox: 0,
  16. safari: 0,
  17. konq: 0,
  18. opera: 0,
  19. chrome: 0,
  20. ver: null
  21. };
  22. // 平台、设备和操作系统
  23. var system = {
  24. win: false,
  25. mac: false,
  26. xll: false, // unix
  27. // 移动设备
  28. iphone: false,
  29. ipod: false,
  30. ipad: false,
  31. ios: false,
  32. android: false,
  33. nokiaN: false,
  34. winMobile: false,
  35. // 游戏系统
  36. wii: false,
  37. ps: false
  38. };
  39. // 检查呈现引擎和浏览器
  40. var ua = navigator.userAgent;
  41. if(window.opera){
  42. engine.ver = browser.ver = window.opera.version();
  43. engine.opera = browser.opera = parseFloat(engine.ver);
  44. } else if (/AppleWebKit\/(\S+)/.test(ua)){
  45. engine.ver = RegExp["$1"];
  46. engine.webkit = parseFloat(engine.ver);
  47. // 确定是Chrome还是Safari
  48. if(/Chrome\/(\S+)/.test(ua)){
  49. browser.ver = RegExp["$1"];
  50. browser.chrome = parseFloat(browser.ver);
  51. }else if (/Version\/(\S+)/.test(ua)){
  52. browser.ver = RegExp["$1"];
  53. browser.safari = parseFloat(browser.ver);
  54. }else {
  55. // 近似地确定版本号
  56. var safariVersion = 1;
  57. if(engine.webkit < 100){
  58. safariVersion = 1;
  59. }else if(engine.webkit < 312){
  60. safariVersion = 1.2;
  61. }else if(engine.webkit < 412){
  62. safariVersion = 1.3;
  63. }else{
  64. safariVersion = 2;
  65. }
  66. browser.safari = browser.ver = safariVersion;
  67. }
  68. } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
  69. engine.ver = browser.ver = RegExp["$1"];
  70. engine.khtml = browser.konq = parseFloat(engine.ver);
  71. } else if(/rv:{[^\)]+}\) Gecko\/\d{8}/.test(ua)) {
  72. engine.ver = RegExp["$1"];
  73. engine.gecko = parseFloat(engine.ver);
  74. // 确定是不是Firefox
  75. if(/Firefox\/(\S+)/.test(ua)){
  76. browser.ver = RegExp["$1"];
  77. browser.firefox = parseFloat(browser.ver);
  78. }
  79. }else if(/MSIE ([^;]+)/.test(ua)){
  80. engine.ver = browser.ver = RegExp["$1"];
  81. engine.ver = browser.ie = parseFloat(engine.ver);
  82. }
  83. // 检测浏览器
  84. browser.ie = engine.ie;
  85. browser.opera = engine.opera;
  86. // 检测平台
  87. var p = navigator.platform;
  88. system.win = p.indexOf("Win") == 0;
  89. system.mac = p.indexOf("Mac") == 0;
  90. system.xll = (p == "Xll") || (p.indexOf("Linux") == 0);
  91. // 检测Windows操作系统
  92. if(system.win){
  93. if(/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)){
  94. if(RegExp["$1"] == "NT"){
  95. switch(RegExp["$2"]){
  96. case "5.0":
  97. system.win = "2000";
  98. break;
  99. case "5.1":
  100. system.win = "XP";
  101. break;
  102. case "6.0":
  103. system.win = "Vista";
  104. break;
  105. case "6.1":
  106. system.win = "7";
  107. break;
  108. default:
  109. system.win = "NT";
  110. break;
  111. }
  112. } else if (RegExp["$1"] == "9x"){
  113. system.win = "ME";
  114. } else {
  115. system.win = RegExp["$1"];
  116. }
  117. }
  118. }
  119. // 移动设备
  120. system.iphone = ua.indexOf("iphone") > -1;
  121. system.ipod = ua.indexOf("ipod") > -1;
  122. system.ipad = ua.indexOf("ipad") > -1;
  123. system.nokiaN = ua.indexOf("NokiaN") > -1;
  124. // window mobile
  125. if(system.win == "CE"){
  126. system.winMobile = system.win;
  127. } else if (system.win == "Ph"){
  128. if(/Window Phone OS (\d+.\d+)/.test(ua)){
  129. system.win = "Phone";
  130. system.winMobile = parseFloat(RegExp["$1"]);
  131. }
  132. }
  133. // 检查IOS版本
  134. if (system.mac && ua.indexOf("Mobile") > -1) {
  135. if (/CPU (?:iPhone)?OS (\d+_\d+)/.test(ua)) {
  136. system.ios = parseFloat(RegExp.$1.replace("_", "."));
  137. } else {
  138. system.ios = 2; // 因为没有办法确定到底是什么版本,所以设置为更早的版本比较稳妥
  139. }
  140. }
  141. // 检查Android版本
  142. if (/Andriod (\d+\.\d+)/.test(ua)) {
  143. system.andriod = parseFloat(RegExp.$1);
  144. }
  145. // 游戏系统
  146. system.wii = ua.indexOf("Wii") > -1;
  147. system.ps = /playstation/i.test(ua);
  148. return {
  149. engine: engine,
  150. browser: browser,
  151. system: system
  152. };
  153. }();