Posted By

onopko on 03/11/10


Tagged

check browser os


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

RM


OS and Modern Browser Check


 / Published in: JavaScript
 

  1. /* ================================================================== */
  2. /* */
  3. /* ! OS and Modern Browser Check */
  4. /* */
  5. /* for Mac OS X, iPhone OS and Windows */
  6. /* Safari, Chrome, Firefox, Opera and Internet Explorer */
  7. /* */
  8. /* ------------------------------------------------------------------
  9. *
  10. * How to get OS Name:
  11. * alert(_ua.os.name);
  12. *
  13. * How to get OS Version (except "Opera" in Mac):
  14. * alert(_ua.os.name);
  15. *
  16. * How to get Browser Name:
  17. * alert(_ua.browser.name);
  18. *
  19. * How to get Browser Version:
  20. * alert(_ua.browser.ver);
  21. *
  22. * =================================================================== */
  23.  
  24. var _ua = new Object();
  25.  
  26. _ua.str = navigator.userAgent.toUpperCase();
  27. _ua.os = {};
  28. _ua.browser = {};
  29.  
  30. if (_ua.str.indexOf("MACINTOSH") >= 0) {
  31. _ua.os.name = "Mac";
  32. if (_ua.str.indexOf("MAC OS X ") >=0 ) {
  33. _ua.os.ver = _ua.str.match(/MAC OS X [0-9_.]*/).toString().substr(9, 4).replace(/_/g, ".");
  34. }
  35. }
  36. else if (_ua.str.indexOf("IPHONE") >= 0) {
  37. _ua.os.name = "iPhone";
  38. if (_ua.str.indexOf("IPHONE OS ") >=0 ) {
  39. _ua.os.ver = _ua.str.match(/IPHONE OS [0-9_.]*/).toString().substr(10, 3).replace(/_/g, ".");
  40. }
  41. }
  42. else if (_ua.str.indexOf("IPOD") >= 0) {
  43. _ua.os.name = "iPod touch";
  44. if (_ua.str.indexOf("IPHONE OS ") >=0 ) {
  45. _ua.os.ver = _ua.str.match(/IPHONE OS [0-9_.]*/).toString().substr(10, 3).replace(/_/g, ".");
  46. }
  47. }
  48. else if (_ua.str.indexOf("WINDOWS") >= 0) {
  49. _ua.os.name = "Windows";
  50. if (_ua.str.indexOf("WINDOWS NT 5.1") >= 0) _ua.os.ver = "XP";
  51. if (_ua.str.indexOf("WINDOWS NT 6.0") >= 0) _ua.os.ver = "Vista";
  52. if (_ua.str.indexOf("WINDOWS NT 6.1") >= 0) _ua.os.ver = "7";
  53. }
  54.  
  55.  
  56. if (_ua.str.indexOf("CHROME") >= 0) {
  57. _ua.browser.name = "Chrome";
  58. _ua.browser.ver = _ua.str.match(/CHROME\/[0-9_.]*/).toString().substr(7,3);
  59. }
  60. else if (_ua.str.indexOf("IPHONE") >= 0 && _ua.str.indexOf("SAFARI") >= 0) {
  61. _ua.browser.name = "Mobile Safari";
  62. _ua.browser.ver = _ua.str.match(/VERSION\/[0-9_.]*/).toString().substr(8);
  63. }
  64. else if (_ua.str.indexOf("IPOD") >= 0 && _ua.str.indexOf("SAFARI") >= 0) {
  65. _ua.browser.name = "Mobile Safari";
  66. _ua.browser.ver = _ua.str.match(/VERSION\/[0-9_.]*/).toString().substr(8);
  67. }
  68. else if (_ua.str.indexOf("SAFARI") >= 0) {
  69. _ua.browser.name = "Safari";
  70. _ua.browser.ver = _ua.str.match(/VERSION\/[0-9_.]*/).toString().substr(8);
  71. }
  72. else if (_ua.str.indexOf("FIREFOX") >= 0) {
  73. _ua.browser.name = "Firefox";
  74. _ua.browser.ver = _ua.str.match(/FIREFOX\/[0-9_.]*/).toString().substr(8, 3);
  75. }
  76. else if (_ua.str.indexOf("OPERA") >= 0) {
  77. _ua.browser.name = "Opera";
  78. _ua.browser.ver = _ua.str.match(/VERSION\/[0-9_.]*/).toString().substr(8);
  79. }
  80. else if (_ua.str.indexOf("MSIE") >= 0) {
  81. _ua.browser.name = "IE";
  82. if (_ua.str.indexOf("MSIE 5.0") >= 0) _ua.browser.ver = "5.0";
  83. if (_ua.str.indexOf("MSIE 5.5") >= 0) _ua.browser.ver = "5.5";
  84. if (_ua.str.indexOf("MSIE 6.0") >= 0) _ua.browser.ver = "6.0";
  85. if (_ua.str.indexOf("MSIE 7.0") >= 0) _ua.browser.ver = "7.0";
  86. if (_ua.str.indexOf("MSIE 8.0") >= 0) _ua.browser.ver = "8.0";
  87. }

Report this snippet  

You need to login to post a comment.