Posted By

pablazo on 02/09/07


Tagged

javascript browser


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

luman
Hirmine
basicmagic
wintondeshong
vali29
creativeboulder
wattsupnow


browser detect


 / Published in: JavaScript
 

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

//The script

//Copy this script into your JavaScript files. It works immediately, and you can query three properties of the BrowserDetect object:

// * Browser name: BrowserDetect.browser // * Browser version: BrowserDetect.version // * OS name: BrowserDetect.OS

  1. //The script
  2.  
  3. //Copy this script into your JavaScript files. It works immediately, and you can query three properties of the BrowserDetect object:
  4.  
  5. // * Browser name: BrowserDetect.browser
  6. // * Browser version: BrowserDetect.version
  7. // * OS name: BrowserDetect.OS
  8.  
  9.  
  10. var BrowserDetect = {
  11. init: function () {
  12. this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
  13. this.version = this.searchVersion(navigator.userAgent)
  14. || this.searchVersion(navigator.appVersion)
  15. || "an unknown version";
  16. this.OS = this.searchString(this.dataOS) || "an unknown OS";
  17. },
  18. searchString: function (data) {
  19. for (var i=0;i<data.length;i++) {
  20. var dataString = data[i].string;
  21. var dataProp = data[i].prop;
  22. this.versionSearchString = data[i].versionSearch || data[i].identity;
  23. if (dataString) {
  24. if (dataString.indexOf(data[i].subString) != -1)
  25. return data[i].identity;
  26. }
  27. else if (dataProp)
  28. return data[i].identity;
  29. }
  30. },
  31. searchVersion: function (dataString) {
  32. var index = dataString.indexOf(this.versionSearchString);
  33. if (index == -1) return;
  34. return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  35. },
  36. dataBrowser: [
  37. { string: navigator.userAgent,
  38. subString: "OmniWeb",
  39. versionSearch: "OmniWeb/",
  40. identity: "OmniWeb"
  41. },
  42. {
  43. string: navigator.vendor,
  44. subString: "Apple",
  45. identity: "Safari"
  46. },
  47. {
  48. prop: window.opera,
  49. identity: "Opera"
  50. },
  51. {
  52. string: navigator.vendor,
  53. subString: "iCab",
  54. identity: "iCab"
  55. },
  56. {
  57. string: navigator.vendor,
  58. subString: "KDE",
  59. identity: "Konqueror"
  60. },
  61. {
  62. string: navigator.userAgent,
  63. subString: "Firefox",
  64. identity: "Firefox"
  65. },
  66. {
  67. string: navigator.vendor,
  68. subString: "Camino",
  69. identity: "Camino"
  70. },
  71. { // for newer Netscapes (6+)
  72. string: navigator.userAgent,
  73. subString: "Netscape",
  74. identity: "Netscape"
  75. },
  76. {
  77. string: navigator.userAgent,
  78. subString: "MSIE",
  79. identity: "Explorer",
  80. versionSearch: "MSIE"
  81. },
  82. {
  83. string: navigator.userAgent,
  84. subString: "Gecko",
  85. identity: "Mozilla",
  86. versionSearch: "rv"
  87. },
  88. { // for older Netscapes (4-)
  89. string: navigator.userAgent,
  90. subString: "Mozilla",
  91. identity: "Netscape",
  92. versionSearch: "Mozilla"
  93. }
  94. ],
  95. dataOS : [
  96. {
  97. string: navigator.platform,
  98. subString: "Win",
  99. identity: "Windows"
  100. },
  101. {
  102. string: navigator.platform,
  103. subString: "Mac",
  104. identity: "Mac"
  105. },
  106. {
  107. string: navigator.platform,
  108. subString: "Linux",
  109. identity: "Linux"
  110. }
  111. ]
  112.  
  113. };
  114. BrowserDetect.init();

Report this snippet  

You need to login to post a comment.