Browser/OS Detection


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

JQuery dependent snippet that will detect OS, Browser and Version


Copy this code and paste it in your HTML
  1. (function()
  2. {
  3.  
  4. var BrowserDetect =
  5. {
  6. init: function ()
  7. {
  8. this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
  9. this.version = this.searchVersion(navigator.userAgent)
  10. || this.searchVersion(navigator.appVersion)
  11. || "an unknown version";
  12. this.OS = this.searchString(this.dataOS) || "an unknown OS";
  13. },
  14. searchString: function (data)
  15. {
  16. for (var i=0;i<data.length;i++)
  17. {
  18. var dataString = data[i].string;
  19. var dataProp = data[i].prop;
  20. this.versionSearchString = data[i].versionSearch || data[i].identity;
  21. if (dataString)
  22. {
  23. if (dataString.indexOf(data[i].subString) != -1)
  24. return data[i].identity;
  25. }
  26. else if (dataProp)
  27. return data[i].identity;
  28. }
  29. },
  30. searchVersion: function (dataString)
  31. {
  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. [
  38. {
  39. string: navigator.userAgent,
  40. subString: "Chrome",
  41. identity: "Chrome"
  42. },
  43. { string: navigator.userAgent,
  44. subString: "OmniWeb",
  45. versionSearch: "OmniWeb/",
  46. identity: "OmniWeb"
  47. },
  48. {
  49. string: navigator.vendor,
  50. subString: "Apple",
  51. identity: "Safari",
  52. versionSearch: "Version"
  53. },
  54. {
  55. prop: window.opera,
  56. identity: "Opera"
  57. },
  58. {
  59. string: navigator.vendor,
  60. subString: "iCab",
  61. identity: "iCab"
  62. },
  63. {
  64. string: navigator.vendor,
  65. subString: "KDE",
  66. identity: "Konqueror"
  67. },
  68. {
  69. string: navigator.userAgent,
  70. subString: "Firefox",
  71. identity: "Firefox"
  72. },
  73. {
  74. string: navigator.vendor,
  75. subString: "Camino",
  76. identity: "Camino"
  77. },
  78. { // for newer Netscapes (6+)
  79. string: navigator.userAgent,
  80. subString: "Netscape",
  81. identity: "Netscape"
  82. },
  83. {
  84. string: navigator.userAgent,
  85. subString: "MSIE",
  86. identity: "IE",
  87. versionSearch: "MSIE"
  88. },
  89. {
  90. string: navigator.userAgent,
  91. subString: "Gecko",
  92. identity: "Mozilla",
  93. versionSearch: "rv"
  94. },
  95. { // for older Netscapes (4-)
  96. string: navigator.userAgent,
  97. subString: "Mozilla",
  98. identity: "Netscape",
  99. versionSearch: "Mozilla"
  100. }
  101. ],
  102. dataOS :
  103. [
  104. {
  105. string: navigator.platform,
  106. subString: "Win",
  107. identity: "Windows"
  108. },
  109. {
  110. string: navigator.platform,
  111. subString: "Mac",
  112. identity: "Mac"
  113. },
  114. {
  115. string: navigator.userAgent,
  116. subString: "iPhone",
  117. identity: "iPhone/iPod"
  118. },
  119. {
  120. string: navigator.platform,
  121. subString: "Linux",
  122. identity: "Linux"
  123. }
  124. ]
  125.  
  126. };
  127.  
  128. BrowserDetect.init();
  129.  
  130. window.$.client =
  131. {
  132. OS : BrowserDetect.OS ,
  133. Browser : BrowserDetect.browser ,
  134. Version : BrowserDetect.version
  135. };
  136.  
  137. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.