verifier.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2016 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Methods to verify IE versions.
  16. * TODO(johnlenz): delete this remove this file on the experiment is complete.
  17. */
  18. goog.provide('goog.labs.useragent.verifier');
  19. /** @const */
  20. goog.labs.useragent.verifier.NOT_IE = 0;
  21. /**
  22. * Detect the the current IE version using runtime behavior, returns 0
  23. * if a version of IE is not detected.
  24. * @return {number}
  25. */
  26. goog.labs.useragent.verifier.detectIeVersionByBehavior = function() {
  27. if (document.all) {
  28. if (!document.compatMode) {
  29. return 5;
  30. }
  31. if (!window.XMLHttpRequest) {
  32. return 6;
  33. }
  34. if (!document.querySelector) {
  35. return 7;
  36. }
  37. if (!document.addEventListener) {
  38. return 8;
  39. }
  40. if (!window.atob) {
  41. return 9;
  42. }
  43. return 10;
  44. }
  45. if (!(window.ActiveXObject) && 'ActiveXObject' in window) {
  46. return 11;
  47. }
  48. return goog.labs.useragent.verifier.NOT_IE;
  49. };
  50. /**
  51. * Detect the the current IE version using MSIE version presented in the
  52. * user agent string (This will not detected IE 11 which does not present a
  53. * MSIE version), or zero if IE is not detected.
  54. * @return {number}
  55. */
  56. goog.labs.useragent.verifier.detectIeVersionByNavigator = function() {
  57. var ua = navigator.userAgent.toLowerCase();
  58. if (ua.indexOf('msie') != -1) {
  59. var value = parseInt(ua.split('msie')[1], 10);
  60. if (typeof value == 'number' && !isNaN(value)) {
  61. return value;
  62. }
  63. }
  64. return goog.labs.useragent.verifier.NOT_IE;
  65. };
  66. /**
  67. * Correct the actual IE version based on the Trident version in the user agent
  68. * string. This adjusts for IE's "compatiblity modes".
  69. * @return {number}
  70. */
  71. goog.labs.useragent.verifier.getCorrectedIEVersionByNavigator = function() {
  72. var ua = navigator.userAgent;
  73. if (/Trident/.test(ua) || /MSIE/.test(ua)) {
  74. return goog.labs.useragent.verifier.getIEVersion_(ua);
  75. } else {
  76. return goog.labs.useragent.verifier.NOT_IE;
  77. }
  78. };
  79. /**
  80. * Get corrected IE version, see goog.labs.userAgent.browser.getIEVersion_
  81. *
  82. * @param {string} userAgent the User-Agent.
  83. * @return {number}
  84. * @private
  85. */
  86. goog.labs.useragent.verifier.getIEVersion_ = function(userAgent) {
  87. // IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
  88. // bug. Example UA:
  89. // Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
  90. // like Gecko.
  91. var rv = /rv: *([\d\.]*)/.exec(userAgent);
  92. if (rv && rv[1]) {
  93. return Number(rv[1]);
  94. }
  95. var msie = /MSIE +([\d\.]+)/.exec(userAgent);
  96. if (msie && msie[1]) {
  97. // IE in compatibility mode usually identifies itself as MSIE 7.0; in this
  98. // case, use the Trident version to determine the version of IE. For more
  99. // details, see the links above.
  100. var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
  101. if (msie[1] == '7.0') {
  102. if (tridentVersion && tridentVersion[1]) {
  103. switch (tridentVersion[1]) {
  104. case '4.0':
  105. return 8;
  106. case '5.0':
  107. return 9;
  108. case '6.0':
  109. return 10;
  110. case '7.0':
  111. return 11;
  112. }
  113. } else {
  114. return 7;
  115. }
  116. } else {
  117. return Number(msie[1]);
  118. }
  119. }
  120. return goog.labs.useragent.verifier.NOT_IE;
  121. };