ua.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. * Full Terms at http://bclary.com/lib/js/license/mpl-tri-license.txt
  4. *
  5. * Software distributed under the License is distributed on an "AS IS" basis,
  6. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  7. * for the specific language governing rights and limitations under the
  8. * License.
  9. *
  10. * The Original Code is Netscape code.
  11. *
  12. * The Initial Developer of the Original Code is
  13. * Netscape Corporation.
  14. * Portions created by the Initial Developer are Copyright (C) 2001
  15. * the Initial Developer. All Rights Reserved.
  16. *
  17. * Contributor(s): Bob Clary <bclary@netscape.com>
  18. *
  19. * ***** END LICENSE BLOCK ***** */
  20. function xbDetectBrowser()
  21. {
  22. var oldOnError = window.onerror;
  23. var element = null;
  24. window.onerror = null;
  25. // work around bug in xpcdom Mozilla 0.9.1
  26. window.saveNavigator = window.navigator;
  27. navigator.OS = '';
  28. navigator.version = parseFloat(navigator.appVersion);
  29. navigator.org = '';
  30. navigator.family = '';
  31. var platform;
  32. if (typeof(window.navigator.platform) != 'undefined')
  33. {
  34. platform = window.navigator.platform.toLowerCase();
  35. if (platform.indexOf('win') != -1)
  36. navigator.OS = 'win';
  37. else if (platform.indexOf('mac') != -1)
  38. navigator.OS = 'mac';
  39. else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
  40. navigator.OS = 'nix';
  41. }
  42. var i = 0;
  43. var ua = window.navigator.userAgent.toLowerCase();
  44. if (ua.indexOf('safari') != -1)
  45. {
  46. i = ua.indexOf('safari');
  47. navigator.family = 'safari';
  48. navigator.org = 'safari';
  49. navigator.version = parseFloat('0' + ua.substr(i+7), 10);
  50. }
  51. else if (ua.indexOf('opera') != -1)
  52. {
  53. i = ua.indexOf('opera');
  54. navigator.family = 'opera';
  55. navigator.org = 'opera';
  56. navigator.version = parseFloat('0' + ua.substr(i+6), 10);
  57. }
  58. else if ((i = ua.indexOf('msie')) != -1)
  59. {
  60. navigator.org = 'microsoft';
  61. navigator.version = parseFloat('0' + ua.substr(i+5), 10);
  62. if (navigator.version < 4)
  63. navigator.family = 'ie3';
  64. else
  65. navigator.family = 'ie4'
  66. }
  67. else if (ua.indexOf('gecko') != -1)
  68. {
  69. navigator.family = 'gecko';
  70. var rvStart = ua.indexOf('rv:');
  71. var rvEnd = ua.indexOf(')', rvStart);
  72. var rv = ua.substring(rvStart+3, rvEnd);
  73. var rvParts = rv.split('.');
  74. var rvValue = 0;
  75. var exp = 1;
  76. for (var i = 0; i < rvParts.length; i++)
  77. {
  78. var val = parseInt(rvParts[i]);
  79. rvValue += val / exp;
  80. exp *= 100;
  81. }
  82. navigator.version = rvValue;
  83. if (ua.indexOf('netscape') != -1)
  84. navigator.org = 'netscape';
  85. else if (ua.indexOf('compuserve') != -1)
  86. navigator.org = 'compuserve';
  87. else
  88. navigator.org = 'mozilla';
  89. }
  90. else if ((ua.indexOf('mozilla') !=-1) && (ua.indexOf('spoofer')==-1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera')==-1)&& (ua.indexOf('webtv')==-1) && (ua.indexOf('hotjava')==-1))
  91. {
  92. var is_major = parseFloat(navigator.appVersion);
  93. if (is_major < 4)
  94. navigator.version = is_major;
  95. else
  96. {
  97. i = ua.lastIndexOf('/')
  98. navigator.version = parseFloat('0' + ua.substr(i+1), 10);
  99. }
  100. navigator.org = 'netscape';
  101. navigator.family = 'nn' + parseInt(navigator.appVersion);
  102. }
  103. else if ((i = ua.indexOf('aol')) != -1 )
  104. {
  105. // aol
  106. navigator.family = 'aol';
  107. navigator.org = 'aol';
  108. navigator.version = parseFloat('0' + ua.substr(i+4), 10);
  109. }
  110. else if ((i = ua.indexOf('hotjava')) != -1 )
  111. {
  112. // hotjava
  113. navigator.family = 'hotjava';
  114. navigator.org = 'sun';
  115. navigator.version = parseFloat(navigator.appVersion);
  116. }
  117. window.onerror = oldOnError;
  118. }
  119. xbDetectBrowser();