Browser Detect (from quirksmode)


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

A useful but often overrated JavaScript function is the browser detect. Sometimes you want to give specific instructions or load a new page in case the viewer uses, for instance, Safari.

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


Copy this code and paste it in your HTML
  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. { string: navigator.userAgent,
  29. subString: "OmniWeb",
  30. versionSearch: "OmniWeb/",
  31. identity: "OmniWeb"
  32. },
  33. {
  34. string: navigator.vendor,
  35. subString: "Apple",
  36. identity: "Safari"
  37. },
  38. {
  39. prop: window.opera,
  40. identity: "Opera"
  41. },
  42. {
  43. string: navigator.vendor,
  44. subString: "iCab",
  45. identity: "iCab"
  46. },
  47. {
  48. string: navigator.vendor,
  49. subString: "KDE",
  50. identity: "Konqueror"
  51. },
  52. {
  53. string: navigator.userAgent,
  54. subString: "Firefox",
  55. identity: "Firefox"
  56. },
  57. {
  58. string: navigator.vendor,
  59. subString: "Camino",
  60. identity: "Camino"
  61. },
  62. { // for newer Netscapes (6+)
  63. string: navigator.userAgent,
  64. subString: "Netscape",
  65. identity: "Netscape"
  66. },
  67. {
  68. string: navigator.userAgent,
  69. subString: "MSIE",
  70. identity: "Explorer",
  71. versionSearch: "MSIE"
  72. },
  73. {
  74. string: navigator.userAgent,
  75. subString: "Gecko",
  76. identity: "Mozilla",
  77. versionSearch: "rv"
  78. },
  79. { // for older Netscapes (4-)
  80. string: navigator.userAgent,
  81. subString: "Mozilla",
  82. identity: "Netscape",
  83. versionSearch: "Mozilla"
  84. }
  85. ],
  86. dataOS : [
  87. {
  88. string: navigator.platform,
  89. subString: "Win",
  90. identity: "Windows"
  91. },
  92. {
  93. string: navigator.platform,
  94. subString: "Mac",
  95. identity: "Mac"
  96. },
  97. {
  98. string: navigator.platform,
  99. subString: "Linux",
  100. identity: "Linux"
  101. }
  102. ]
  103.  
  104. };
  105. BrowserDetect.init();

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.