browser.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * jQBrowser v0.2 - Extend jQuery's browser detection capabilities
  3. * * http://davecardwell.co.uk/javascript/jquery/plugins/jquery-browserdetect/0.2/
  4. *
  5. * Dave Cardwell <http://davecardwell.co.uk/>
  6. *
  7. * Built on the shoulders of giants:
  8. * * John Resig <http://jquery.com/>
  9. * * Peter-Paul Koch <http://www.quirksmode.org/?/js/detect.html>
  10. *
  11. *
  12. * Copyright (c) 2006 Dave Cardwell, dual licensed under the MIT and GPL
  13. * licenses:
  14. * * http://www.opensource.org/licenses/mit-license.php
  15. * * http://www.gnu.org/licenses/gpl.txt
  16. */
  17. /**
  18. * For the latest version of this plugin, and a discussion of its usage and
  19. * implementation, visit:
  20. * * http://davecardwell.co.uk/javascript/jquery/plugins/jquery-browserdetect/
  21. */
  22. new function() {
  23. /**
  24. * The following functions and attributes form the Public interface of the
  25. * jQBrowser plugin, accessed externally through the $.browser object.
  26. * See the relevant function definition later in the source for further
  27. * information.
  28. *
  29. * $.browser.browser()
  30. * $.browser.version.number()
  31. * $.browser.version.string()
  32. * $.browser.OS()
  33. *
  34. * $.browser.aol()
  35. * $.browser.camino()
  36. * $.browser.firefox()
  37. * $.browser.flock()
  38. * $.browser.icab()
  39. * $.browser.konqueror()
  40. * $.browser.mozilla()
  41. * $.browser.msie()
  42. * $.browser.netscape()
  43. * $.browser.opera()
  44. * $.browser.safari()
  45. *
  46. * $.browser.linux()
  47. * $.browser.mac()
  48. * $.browser.win()
  49. */
  50. var Public = {
  51. // The current browser, its version as a number or a string, and the
  52. // operating system its running on.
  53. 'browser': function() {
  54. return Private.browser;
  55. },
  56. 'version': {
  57. 'number': function() {
  58. return Math.floor(Private.version.number);
  59. },
  60. 'string': function() {
  61. return Private.version.string;
  62. }
  63. },
  64. 'OS': function() {
  65. return Private.OS;
  66. },
  67. // A boolean value indicating whether or not the given browser was
  68. // detected.
  69. 'chrome': function() {
  70. return Private.chrome;
  71. },
  72. 'aol': function() {
  73. return Private.aol;
  74. },
  75. 'camino': function() {
  76. return Private.camino;
  77. },
  78. 'firefox': function() {
  79. return Private.firefox;
  80. },
  81. 'flock': function() {
  82. return Private.flock;
  83. },
  84. 'icab': function() {
  85. return Private.icab;
  86. },
  87. 'konqueror': function() {
  88. return Private.konqueror;
  89. },
  90. 'mozilla': function() {
  91. return Private.mozilla;
  92. },
  93. 'msie': function() {
  94. return Private.msie;
  95. },
  96. 'netscape': function() {
  97. return Private.netscape;
  98. },
  99. 'opera': function() {
  100. return Private.opera;
  101. },
  102. 'safari': function() {
  103. return Private.safari;
  104. },
  105. // A boolean value indicating whether or not the given OS was
  106. // detected.
  107. 'linux': function() {
  108. return Private.linux;
  109. },
  110. 'mac': function() {
  111. return Private.mac;
  112. },
  113. 'win': function() {
  114. return Private.win;
  115. }
  116. };
  117. // Allow external access to the 'Public' interface through the $.browser
  118. // object.
  119. $.browser = Public;
  120. /**
  121. * The following functions and attributes form the internal methods and
  122. * state of the jQBrowser plugin. See the relevant function definition
  123. * later in the source for further information.
  124. *
  125. * Private.browser
  126. * Private.version
  127. * Private.OS
  128. *
  129. * Private.aol
  130. * Private.camino
  131. * Private.firefox
  132. * Private.flock
  133. * Private.icab
  134. * Private.konqueror
  135. * Private.mozilla
  136. * Private.msie
  137. * Private.netscape
  138. * Private.opera
  139. * Private.safari
  140. *
  141. * Private.linux
  142. * Private.mac
  143. * Private.win
  144. */
  145. var Private = {
  146. // Initially set to 'Unknown', if detected each of these properties will
  147. // be updated.
  148. 'browser': 'Unknown',
  149. 'version': {
  150. 'number': undefined,
  151. 'string': 'Unknown'
  152. },
  153. 'OS': 'Unknown',
  154. // Initially set to false, if detected one of the following browsers
  155. // will be updated.
  156. 'chrome': false,
  157. 'aol': false,
  158. 'camino': false,
  159. 'firefox': false,
  160. 'flock': false,
  161. 'icab': false,
  162. 'konqueror': false,
  163. 'mozilla': false,
  164. 'msie': false,
  165. 'netscape': false,
  166. 'opera': false,
  167. 'safari': false,
  168. // Initially set to false, if detected one of the following operating
  169. // systems will be updated.
  170. 'linux': false,
  171. 'mac': false,
  172. 'win': false
  173. };
  174. /**
  175. * Loop over the items in 'data' trying to find a browser match with the
  176. * test in data[i].browser(). Once found, attempt to determine the
  177. * browser version.
  178. *
  179. * 'name': A string containing the full name of the browser.
  180. * 'identifier': By default this is a lowercase version of 'name', but
  181. * this can be overwritten by explicitly defining an
  182. * 'identifier'.
  183. * 'browser': A function that returns a boolean value indicating
  184. * whether or not the given browser is detected.
  185. * 'version': An optional function that overwrites the default version
  186. * testing. Must return the result of a .match().
  187. *
  188. * Please note that the order of the data array is important, as some
  189. * browsers contain details of others in their navigator.userAgent string.
  190. * For example, Flock's contains 'Firefox' so much come before Firefox's
  191. * test to avoid false positives.
  192. */
  193. for( var i = 0, // counter
  194. ua = navigator.userAgent, // the navigator's user agent string
  195. ve = navigator.vendor, // the navigator's vendor string
  196. data = [ // browser tests and data
  197. { // Safari <http://www.apple.com/safari/>
  198. 'name': 'Chrome',
  199. 'browser': function() {
  200. return /Google/.test(ve)
  201. }
  202. },
  203. { // Safari <http://www.apple.com/safari/>
  204. 'name': 'Safari',
  205. 'browser': function() {
  206. return /Apple/.test(ve)
  207. }
  208. },
  209. { // Opera <http://www.opera.com/>
  210. 'name': 'Opera',
  211. 'browser': function() {
  212. return window.opera != undefined
  213. }
  214. },
  215. { // iCab <http://www.icab.de/>
  216. 'name': 'iCab',
  217. 'browser': function() {
  218. return /iCab/.test(ve)
  219. }
  220. },
  221. { // Konqueror <http://www.konqueror.org/>
  222. 'name': 'Konqueror',
  223. 'browser': function() {
  224. return /KDE/.test(ve)
  225. }
  226. },
  227. { // AOL Explorer <http://downloads.channel.aol.com/browser>
  228. 'identifier': 'aol',
  229. 'name': 'AOL Explorer',
  230. 'browser': function() {
  231. return /America Online Browser/.test(ua)
  232. },
  233. 'version': function() {
  234. return ua.match(/rev(\d+(?:\.\d+)+)/)
  235. }
  236. },
  237. { // Flock <http://www.flock.com/>
  238. 'name': 'Flock',
  239. 'browser': function() {
  240. return /Flock/.test(ua)
  241. }
  242. },
  243. { // Camino <http://www.caminobrowser.org/>
  244. 'name': 'Camino',
  245. 'browser': function() {
  246. return /Camino/.test(ve)
  247. }
  248. },
  249. { // Firefox <http://www.mozilla.com/firefox/>
  250. 'name': 'Firefox',
  251. 'browser': function() {
  252. return /Firefox/.test(ua)
  253. }
  254. },
  255. { // Netscape <http://browser.netscape.com/>
  256. 'name': 'Netscape',
  257. 'browser': function() {
  258. return /Netscape/.test(ua)
  259. }
  260. },
  261. { // Internet Explorer <http://www.microsoft.com/windows/ie/>
  262. // <http://www.microsoft.com/mac/ie/>
  263. 'identifier': 'msie',
  264. 'name': 'Internet Explorer',
  265. 'browser': function() {
  266. return /MSIE/.test(ua)
  267. },
  268. 'version': function() {
  269. return ua.match(
  270. /MSIE (\d+(?:\.\d+)+(?:b\d*)?)/
  271. )
  272. }
  273. },
  274. { // Mozilla <http://www.mozilla.org/products/mozilla1.x/>
  275. 'name': 'Mozilla',
  276. 'browser': function() {
  277. return /Gecko|Mozilla/.test(ua)
  278. },
  279. 'version': function() {
  280. return ua.match(/rv:(\d+(?:\.\d+)+)/);
  281. }
  282. }
  283. ];
  284. i < data.length;
  285. i++
  286. ) {
  287. if( data[i].browser() ) { // we have a match
  288. // If the identifier is not explicitly set, use a lowercase
  289. // version of the given name.
  290. var identifier = data[i].identifier ? data[i].identifier
  291. : data[i].name.toLowerCase();
  292. // Make a note that this browser was detected.
  293. Private[ identifier ] = true;
  294. // $.browser.browser() will now return the correct browser.
  295. Private.browser = data[i].name;
  296. var result;
  297. if( data[i].version != undefined && (result = data[i].version()) ) {
  298. // Use the explicitly set test for browser version.
  299. Private.version.string = result[1];
  300. Private.version.number = parseFloat( result[1] );
  301. } else {
  302. // Otherwise use the default test which searches for the
  303. // version number after the browser name in the user agent
  304. // string.
  305. var re = new RegExp(
  306. data[i].name + '(?:\\s|\\/)(\\d+(?:\\.\\d+)+(?:(?:a|b)\\d*)?)'
  307. );
  308. result = ua.match(re);
  309. if( result != undefined ) {
  310. Private.version.string = result[1];
  311. Private.version.number = parseFloat( result[1] );
  312. }
  313. }
  314. // Once we've detected the browser there is no need to check the
  315. // others.
  316. break;
  317. }
  318. };
  319. /**
  320. * Loop over the items in 'data' trying to find a operating system match
  321. * with the test in data[i].os().
  322. *
  323. * 'name': A string containing the full name of the operating
  324. * system.
  325. * 'identifier': By default this is a lowercase version of 'name', but
  326. * this can be overwritten by explicitly defining an
  327. * 'identifier'.
  328. * 'OS': A function that returns a boolean value indicating
  329. * whether or not the given operating system is detected.
  330. */
  331. for( var i = 0, // counter
  332. pl = navigator.platform, // the navigator's platform string
  333. data = [ // OS data and tests
  334. { // Microsoft Windows <http://www.microsoft.com/windows/>
  335. 'identifier': 'win',
  336. 'name': 'Windows',
  337. 'OS': function() {
  338. return /Win/.test(pl)
  339. }
  340. },
  341. { // Apple Mac OS <http://www.apple.com/macos/>
  342. 'name': 'Mac',
  343. 'OS': function() {
  344. return /Mac/.test(pl)
  345. }
  346. },
  347. { // Linux <http://www.linux.org/>
  348. 'name': 'Linux',
  349. 'OS': function() {
  350. return /Linux/.test(pl)
  351. }
  352. }
  353. ];
  354. i < data.length;
  355. i++
  356. ) {
  357. if( data[i].OS() ) { // we have a match
  358. // If the identifier is not explicitly set, use a lowercase
  359. // version of the given name.
  360. var identifier = data[i].identifier ? data[i].identifier
  361. : data[i].name.toLowerCase();
  362. // Make a note that the OS was detected.
  363. Private[ identifier ] = true;
  364. // $.browser.OS() will now return the correct OS.
  365. Private.OS = data[i].name;
  366. // Once we've detected the browser there is no need to check the
  367. // others.
  368. break;
  369. }
  370. };
  371. }();