Path: blob/trunk/third_party/closure/goog/labs/useragent/verifier.js
4565 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Methods to verify IE versions.8* TODO(johnlenz): delete this remove this file on the experiment is complete.9*/1011goog.module('goog.labs.useragent.verifier');12goog.module.declareLegacyNamespace();1314/** @const */15const NOT_IE = 0;1617/**18* Detect the current IE version using runtime behavior, returns 0 if a version19* of IE is not detected.20* @return {number}21*/22function detectIeVersionByBehavior() {23if (document.all) {24if (!document.compatMode) {25return 5;26}27if (!window.XMLHttpRequest) {28return 6;29}30if (!document.querySelector) {31return 7;32}33if (!document.addEventListener) {34return 8;35}36if (!window.atob) {37return 9;38}3940return 10;41}42if (!(window.ActiveXObject) && 'ActiveXObject' in window) {43return 11;44}4546return NOT_IE;47}4849/**50* Detect the current IE version using MSIE version presented in the user agent51* string (This will not detected IE 11 which does not present a MSIE version),52* or zero if IE is not detected.53* @return {number}54*/55function detectIeVersionByNavigator() {56const ua = navigator.userAgent.toLowerCase();57if (ua.indexOf('msie') != -1) {58const value = parseInt(ua.split('msie')[1], 10);59if (typeof value == 'number' && !isNaN(value)) {60return value;61}62}6364return NOT_IE;65}6667/**68* Correct the actual IE version based on the Trident version in the user agent69* string. This adjusts for IE's "compatiblity modes".70* @return {number}71*/72function getCorrectedIEVersionByNavigator() {73const ua = navigator.userAgent;74if (/Trident/.test(ua) || /MSIE/.test(ua)) {75return getIEVersion(ua);76} else {77return NOT_IE;78}79}8081/**82* Get corrected IE version, see goog.labs.userAgent.browser.getIEVersion_83* @param {string} userAgent the User-Agent.84* @return {number}85*/86function getIEVersion(userAgent) {87// IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade88// bug. Example UA:89// Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)90// like Gecko.91const rv = /rv: *([\d\.]*)/.exec(userAgent);92if (rv && rv[1]) {93return Number(rv[1]);94}9596const msie = /MSIE +([\d\.]+)/.exec(userAgent);97if (msie && msie[1]) {98// IE in compatibility mode usually identifies itself as MSIE 7.0; in this99// case, use the Trident version to determine the version of IE. For more100// details, see the links above.101const tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);102if (msie[1] == '7.0') {103if (tridentVersion && tridentVersion[1]) {104switch (tridentVersion[1]) {105case '4.0':106return 8;107case '5.0':108return 9;109case '6.0':110return 10;111case '7.0':112return 11;113}114} else {115return 7;116}117} else {118return Number(msie[1]);119}120}121return NOT_IE;122}123124exports = {125NOT_IE,126detectIeVersionByBehavior,127detectIeVersionByNavigator,128getCorrectedIEVersionByNavigator,129};130131132