Browser Detection


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

Browser Detection


Copy this code and paste it in your HTML
  1. function _setBrowser()
  2. {
  3. var userAgent = navigator.userAgent.toLowerCase();
  4.  
  5. // Figure out what browser is being used
  6. jQuery.browser = {
  7.  
  8. version: (userAgent.match( /.+(?:rv|it|ra|ie|me|ve)[\/: ]([\d.]+)/ ) || [])[1],
  9.  
  10. chrome: /chrome/.test( userAgent ),
  11. safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
  12. opera: /opera/.test( userAgent ),
  13. firefox: /firefox/.test( userAgent ),
  14. msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
  15.  
  16. mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ),
  17.  
  18. webkit: $.browser.webkit,
  19. gecko: /[^like]{4} gecko/.test( userAgent ),
  20. presto: /presto/.test( userAgent ),
  21.  
  22. xoom: /xoom/.test( userAgent ),
  23.  
  24. android: /android/.test( userAgent ),
  25. androidVersion: (userAgent.match( /.+(?:android)[\/: ]([\d.]+)/ ) || [0,0])[1],
  26.  
  27. iphone: /iphone|ipod/.test( userAgent ),
  28. iphoneVersion: (userAgent.match( /.+(?:iphone\ os)[\/: ]([\d_]+)/ ) || [0,0])[1].toString().split('_').join('.'),
  29.  
  30. ipad: /ipad/.test( userAgent ),
  31. ipadVersion: (userAgent.match( /.+(?:cpu\ os)[\/: ]([\d_]+)/ ) || [0,0])[1].toString().split('_').join('.'),
  32.  
  33. blackberry: /blackberry/.test( userAgent ),
  34.  
  35. winMobile: /Windows\ Phone/.test( userAgent ),
  36. winMobileVersion: (userAgent.match( /.+(?:windows\ phone\ os)[\/: ]([\d_]+)/ ) || [0,0])[1]
  37. };
  38.  
  39. jQuery.browser.mobile = ($.browser.iphone || $.browser.ipad || $.browser.android || $.browser.blackberry );
  40. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.