Posted By

wizard04 on 09/11/08


Tagged

javascript browser detect


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

kcmr
alvaroisorna


PPK's Browser Detect


 / Published in: JavaScript
 

URL: http://www.quirksmode.org/js/detect.html

Modified just a bit. This version includes the Google Chrome beta.

  1. //a simple check for Internet Explorer: use conditional comments in the HTML to set this to the version number
  2. //var IEVersion;
  3.  
  4. //PPK's browser detect script, modified
  5. //http://www.quirksmode.org/js/detect.html
  6. var BrowserDetect = function()
  7. {
  8. //***** private properties and methods *****//
  9.  
  10. var versionSearchString;
  11. function searchString(data)
  12. {
  13. var dataString, dataProp;
  14. for(var i=0; i<data.length; i++)
  15. {
  16. dataString = data[i].string;
  17. dataProp = data[i].prop;
  18. versionSearchString = data[i].versionSearch || data[i].identity;
  19. if(dataString)
  20. {
  21. if(dataString.indexOf(data[i].subString) != -1) return data[i].identity;
  22. }
  23. else if(dataProp) return data[i].identity;
  24. }
  25. }
  26. function searchVersion(dataString)
  27. {
  28. var index = dataString.indexOf(versionSearchString);
  29. if(index == -1) return;
  30. return parseFloat(dataString.substring(index+versionSearchString.length+1));
  31. }
  32.  
  33. var dataBrowser = [
  34. {
  35. string: navigator.userAgent,
  36. subString: "Chrome",
  37. identity: "Chrome"
  38. },
  39. {
  40. string: navigator.userAgent,
  41. subString: "OmniWeb",
  42. versionSearch: "OmniWeb/",
  43. identity: "OmniWeb"
  44. },
  45. {
  46. string: navigator.vendor,
  47. subString: "Apple",
  48. identity: "Safari"
  49. },
  50. {
  51. prop: window.opera,
  52. identity: "Opera"
  53. },
  54. {
  55. string: navigator.vendor,
  56. subString: "iCab",
  57. identity: "iCab"
  58. },
  59. {
  60. string: navigator.vendor,
  61. subString: "KDE",
  62. identity: "Konqueror"
  63. },
  64. {
  65. string: navigator.userAgent,
  66. subString: "Firefox",
  67. identity: "Firefox"
  68. },
  69. {
  70. string: navigator.vendor,
  71. subString: "Camino",
  72. identity: "Camino"
  73. },
  74. { // for newer Netscapes (6+)
  75. string: navigator.userAgent,
  76. subString: "Netscape",
  77. identity: "Netscape"
  78. },
  79. {
  80. string: navigator.userAgent,
  81. subString: "MSIE",
  82. identity: "Explorer",
  83. versionSearch: "MSIE"
  84. },
  85. {
  86. string: navigator.userAgent,
  87. subString: "Gecko",
  88. identity: "Mozilla",
  89. versionSearch: "rv"
  90. },
  91. { // for older Netscapes (4-)
  92. string: navigator.userAgent,
  93. subString: "Mozilla",
  94. identity: "Netscape",
  95. versionSearch: "Mozilla"
  96. }
  97. ];
  98. var dataOS = [
  99. {
  100. string: navigator.platform,
  101. subString: "Win",
  102. identity: "Windows"
  103. },
  104. {
  105. string: navigator.platform,
  106. subString: "Mac",
  107. identity: "Mac"
  108. },
  109. {
  110. string: navigator.platform,
  111. subString: "Linux",
  112. identity: "Linux"
  113. }
  114. ];
  115.  
  116. //***** public properties and methods *****//
  117.  
  118. return {
  119. browser: searchString(dataBrowser) || "",
  120. version: searchVersion(navigator.userAgent) || searchVersion(navigator.appVersion) || "",
  121. OS: searchString(dataOS) || ""
  122. };
  123. }(); //initialize BrowserDetect

Report this snippet  

You need to login to post a comment.