Posted By

elightbo on 11/07/06


Tagged

browserdetct


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


Javascript browser detect lite


 / Published in: JavaScript
 

  1. // JavaScript Document
  2. // if IE5.5+ on Win32, then display PNGs with AlphaImageLoader
  3. var pngAlpha
  4. var pngNormal
  5. //var browser = this.browser();
  6. // Browser Detect Lite v2.1
  7. // http://www.dithered.com/javascript/browser_detect/index.html
  8. // modified by Chris Nott ([email protected] - remove NOSPAM)
  9. //
  10. // modified by Michael Lovitt to include OmniWeb and Dreamcast
  11.  
  12. function BrowserDetectLite() {
  13. var ua = navigator.userAgent.toLowerCase();
  14. this.ua = ua;
  15.  
  16. // browser name
  17. this.isGecko = (ua.indexOf('gecko') != -1);
  18. this.isMozilla = (this.isGecko && ua.indexOf("gecko/") + 14 == ua.length);
  19. this.isNS = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
  20. this.isIE = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) );
  21. this.isOpera = (ua.indexOf("opera") != -1);
  22. this.isKonqueror = (ua.indexOf("konqueror") != -1);
  23. this.isIcab = (ua.indexOf("icab") != -1);
  24. this.isAol = (ua.indexOf("aol") != -1);
  25. this.isWebtv = (ua.indexOf("webtv") != -1);
  26. this.isOmniweb = (ua.indexOf("omniweb") != -1);
  27. this.isDreamcast = (ua.indexOf("dreamcast") != -1);
  28.  
  29. // spoofing and compatible browsers
  30. this.isIECompatible = ( (ua.indexOf("msie") != -1) && !this.isIE);
  31. this.isNSCompatible = ( (ua.indexOf("mozilla") != -1) && !this.isNS && !this.isMozilla);
  32.  
  33. // browser version
  34. this.versionMinor = parseFloat(navigator.appVersion);
  35.  
  36. // correct version number for NS6+
  37. if (this.isNS && this.isGecko) {
  38. this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );
  39. }
  40.  
  41. // correct version number for IE4+
  42. else if (this.isIE && this.versionMinor >= 4) {
  43. this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
  44. }
  45.  
  46. // correct version number for Opera
  47. else if (this.isOpera) {
  48. if (ua.indexOf('opera/') != -1) {
  49. this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera/') + 6 ) );
  50. }
  51. else {
  52. this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera ') + 6 ) );
  53. }
  54. }
  55.  
  56. // correct version number for Konqueror
  57. else if (this.isKonqueror) {
  58. this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
  59. }
  60.  
  61. // correct version number for iCab
  62. else if (this.isIcab) {
  63. if (ua.indexOf('icab/') != -1) {
  64. this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab/') + 6 ) );
  65. }
  66. else {
  67. this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab ') + 6 ) );
  68. }
  69. }
  70.  
  71. // correct version number for WebTV
  72. else if (this.isWebtv) {
  73. this.versionMinor = parseFloat( ua.substring( ua.indexOf('webtv/') + 6 ) );
  74. }
  75.  
  76. this.versionMajor = parseInt(this.versionMinor);
  77. this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
  78.  
  79. // platform
  80. this.isWin = (ua.indexOf('win') != -1);
  81. this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1) );
  82. this.isMac = (ua.indexOf('mac') != -1);
  83. this.isUnix = (ua.indexOf('unix') != -1 || ua.indexOf('linux') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
  84.  
  85. // specific browser shortcuts
  86. this.isNS4x = (this.isNS && this.versionMajor == 4);
  87. this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
  88. this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
  89. this.isNS4up = (this.isNS && this.versionMinor >= 4);
  90. this.isNS6x = (this.isNS && this.versionMajor == 6);
  91. this.isNS6up = (this.isNS && this.versionMajor >= 6);
  92.  
  93. this.isIE4x = (this.isIE && this.versionMajor == 4);
  94. this.isIE4up = (this.isIE && this.versionMajor >= 4);
  95. this.isIE5x = (this.isIE && this.versionMajor == 5);
  96. this.isIE55 = (this.isIE && this.versionMinor == 5.5);
  97. this.isIE5up = (this.isIE && this.versionMajor >= 5);
  98. this.isIE6x = (this.isIE && this.versionMajor == 6);
  99. this.isIE6up = (this.isIE && this.versionMajor >= 6);
  100.  
  101. this.isIE4xMac = (this.isIE4x && this.isMac);
  102. }
  103. var browser = new BrowserDetectLite();

Report this snippet  

You need to login to post a comment.