UserAgent Information detect.js


/ Published in: JavaScript
Save to your folder(s)

判断客户端的浏览器和操作系统的类型和版本号


Copy this code and paste it in your HTML
  1. var sUserAgent = navigator.userAgent;
  2. var fAppVersion = parseFloat(navigator.appVersion);
  3.  
  4. function compareVersions(sVersion1, sVersion2)
  5. {
  6.  
  7. var aVersion1 = sVersion1.split(".");
  8. var aVersion2 = sVersion2.split(".");
  9.  
  10. if (aVersion1.length > aVersion2.length)
  11. {
  12. for (var i=0; i < aVersion1.length - aVersion2.length; i++)
  13. {
  14. aVersion2.push("0");
  15. }
  16. }
  17. else if (aVersion1.length < aVersion2.length)
  18. {
  19. for (var i=0; i < aVersion2.length - aVersion1.length; i++)
  20. {
  21. aVersion1.push("0");
  22. }
  23. }
  24.  
  25. for (var i=0; i < aVersion1.length; i++)
  26. {
  27.  
  28. if (aVersion1[i] < aVersion2[i])
  29. {
  30. return -1;
  31. }
  32. else if (aVersion1[i] > aVersion2[i])
  33. {
  34. return 1;
  35. }
  36. }
  37.  
  38. return 0;
  39.  
  40. }
  41.  
  42. var isOpera = sUserAgent.indexOf("Opera") > -1;
  43. var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
  44.  
  45. if (isOpera)
  46. {
  47. var fOperaVersion;
  48. if(navigator.appName == "Opera")
  49. {
  50. fOperaVersion = fAppVersion;
  51. }
  52. else
  53. {
  54. var reOperaVersion = new RegExp("Opera (\\d+\\.\\d+)");
  55. reOperaVersion.test(sUserAgent);
  56. fOperaVersion = parseFloat(RegExp["$1"]);
  57. }
  58.  
  59. isMinOpera4 = fOperaVersion >= 4;
  60. isMinOpera5 = fOperaVersion >= 5;
  61. isMinOpera6 = fOperaVersion >= 6;
  62. isMinOpera7 = fOperaVersion >= 7;
  63. isMinOpera7_5 = fOperaVersion >= 7.5;
  64. }
  65.  
  66. var isKHTML = sUserAgent.indexOf("KHTML") > -1
  67. || sUserAgent.indexOf("Konqueror") > -1
  68. || sUserAgent.indexOf("AppleWebKit") > -1;
  69.  
  70. var isMinSafari1 = isMinSafari1_2 = false;
  71. var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
  72.  
  73. if (isKHTML)
  74. {
  75. isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
  76. isKonq = sUserAgent.indexOf("Konqueror") > -1;
  77.  
  78. if (isSafari)
  79. {
  80. var reAppleWebKit = new RegExp("AppleWebKit\\/(\\d+(?:\\.\\d*)?)");
  81. reAppleWebKit.test(sUserAgent);
  82. var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
  83.  
  84. isMinSafari1 = fAppleWebKitVersion >= 85;
  85. isMinSafari1_2 = fAppleWebKitVersion >= 124;
  86. }
  87. else if (isKonq)
  88. {
  89.  
  90. var reKonq = new RegExp("Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)");
  91. reKonq.test(sUserAgent);
  92. isMinKonq2_2 = compareVersions(RegExp["$1"], "2.2") >= 0;
  93. isMinKonq3 = compareVersions(RegExp["$1"], "3.0") >= 0;
  94. isMinKonq3_1 = compareVersions(RegExp["$1"], "3.1") >= 0;
  95. isMinKonq3_2 = compareVersions(RegExp["$1"], "3.2") >= 0;
  96. }
  97.  
  98. }
  99.  
  100. var isIE = sUserAgent.indexOf("compatible") > -1
  101. && sUserAgent.indexOf("MSIE") > -1
  102. && !isOpera;
  103.  
  104. var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = false;
  105.  
  106. if (isIE)
  107. {
  108. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  109. reIE.test(sUserAgent);
  110. var fIEVersion = parseFloat(RegExp["$1"]);
  111.  
  112. isMinIE4 = fIEVersion >= 4;
  113. isMinIE5 = fIEVersion >= 5;
  114. isMinIE5_5 = fIEVersion >= 5.5;
  115. isMinIE6 = fIEVersion >= 6.0;
  116. }
  117.  
  118. var isMoz = sUserAgent.indexOf("Gecko") > -1
  119. && !isKHTML;
  120.  
  121. var isMinMoz1 = sMinMoz1_4 = isMinMoz1_5 = false;
  122.  
  123. if (isMoz)
  124. {
  125. var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
  126. reMoz.test(sUserAgent);
  127. isMinMoz1 = compareVersions(RegExp["$1"], "1.0") >= 0;
  128. isMinMoz1_4 = compareVersions(RegExp["$1"], "1.4") >= 0;
  129. isMinMoz1_5 = compareVersions(RegExp["$1"], "1.5") >= 0;
  130. }
  131.  
  132. var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML
  133. && (sUserAgent.indexOf("Mozilla") == 0)
  134. && (navigator.appName == "Netscape")
  135. && (fAppVersion >= 4.0 && fAppVersion < 5.0);
  136.  
  137. var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;
  138.  
  139. if (isNS4)
  140. {
  141. isMinNS4 = true;
  142. isMinNS4_5 = fAppVersion >= 4.5;
  143. isMinNS4_7 = fAppVersion >= 4.7;
  144. isMinNS4_8 = fAppVersion >= 4.8;
  145. }
  146.  
  147. var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
  148. var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
  149. || (navigator.platform == "Macintosh");
  150.  
  151. var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
  152.  
  153. var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false;
  154. var isMac68K = isMacPPC = false;
  155. var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
  156.  
  157. if (isWin)
  158. {
  159. isWin95 = sUserAgent.indexOf("Win95") > -1
  160. || sUserAgent.indexOf("Windows 95") > -1;
  161. isWin98 = sUserAgent.indexOf("Win98") > -1
  162. || sUserAgent.indexOf("Windows 98") > -1;
  163. isWinME = sUserAgent.indexOf("Win 9x 4.90") > -1
  164. || sUserAgent.indexOf("Windows ME") > -1;
  165. isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1
  166. || sUserAgent.indexOf("Windows 2000") > -1;
  167. isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1
  168. || sUserAgent.indexOf("Windows XP") > -1;
  169. isWinNT4 = sUserAgent.indexOf("WinNT") > -1
  170. || sUserAgent.indexOf("Windows NT") > -1
  171. || sUserAgent.indexOf("WinNT4.0") > -1
  172. || sUserAgent.indexOf("Windows NT 4.0") > -1
  173. && (!isWinME && !isWin2K && !isWinXP);
  174. }
  175.  
  176. if (isMac)
  177. {
  178. isMac68K = sUserAgent.indexOf("Mac_68000") > -1
  179. || sUserAgent.indexOf("68K") > -1;
  180. isMacPPC = sUserAgent.indexOf("Mac_PowerPC") > -1
  181. || sUserAgent.indexOf("PPC") > -1;
  182. }
  183.  
  184. if (isUnix)
  185. {
  186. isSunOS = sUserAgent.indexOf("SunOS") > -1;
  187.  
  188. if (isSunOS)
  189. {
  190. var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)");
  191. reSunOS.test(sUserAgent);
  192. isMinSunOS4 = compareVersions(RegExp["$1"], "4.0") >= 0;
  193. isMinSunOS5 = compareVersions(RegExp["$1"], "5.0") >= 0;
  194. isMinSunOS5_5 = compareVersions(RegExp["$1"], "5.5") >= 0;
  195. }
  196. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.