Path: blob/trunk/third_party/closure/goog/labs/useragent/verifier.js
2868 views
// Copyright 2016 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Methods to verify IE versions.16* TODO(johnlenz): delete this remove this file on the experiment is complete.17*/18goog.provide('goog.labs.useragent.verifier');192021/** @const */22goog.labs.useragent.verifier.NOT_IE = 0;232425/**26* Detect the the current IE version using runtime behavior, returns 027* if a version of IE is not detected.28* @return {number}29*/30goog.labs.useragent.verifier.detectIeVersionByBehavior = function() {31if (document.all) {32if (!document.compatMode) {33return 5;34}35if (!window.XMLHttpRequest) {36return 6;37}38if (!document.querySelector) {39return 7;40}41if (!document.addEventListener) {42return 8;43}44if (!window.atob) {45return 9;46}4748return 10;49}50if (!(window.ActiveXObject) && 'ActiveXObject' in window) {51return 11;52}5354return goog.labs.useragent.verifier.NOT_IE;55};565758/**59* Detect the the current IE version using MSIE version presented in the60* user agent string (This will not detected IE 11 which does not present a61* MSIE version), or zero if IE is not detected.62* @return {number}63*/64goog.labs.useragent.verifier.detectIeVersionByNavigator = function() {65var ua = navigator.userAgent.toLowerCase();66if (ua.indexOf('msie') != -1) {67var value = parseInt(ua.split('msie')[1], 10);68if (typeof value == 'number' && !isNaN(value)) {69return value;70}71}7273return goog.labs.useragent.verifier.NOT_IE;74};757677/**78* Correct the actual IE version based on the Trident version in the user agent79* string. This adjusts for IE's "compatiblity modes".80* @return {number}81*/82goog.labs.useragent.verifier.getCorrectedIEVersionByNavigator = function() {83var ua = navigator.userAgent;84if (/Trident/.test(ua) || /MSIE/.test(ua)) {85return goog.labs.useragent.verifier.getIEVersion_(ua);86} else {87return goog.labs.useragent.verifier.NOT_IE;88}89};909192/**93* Get corrected IE version, see goog.labs.userAgent.browser.getIEVersion_94*95* @param {string} userAgent the User-Agent.96* @return {number}97* @private98*/99goog.labs.useragent.verifier.getIEVersion_ = function(userAgent) {100// IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade101// bug. Example UA:102// Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)103// like Gecko.104var rv = /rv: *([\d\.]*)/.exec(userAgent);105if (rv && rv[1]) {106return Number(rv[1]);107}108109var msie = /MSIE +([\d\.]+)/.exec(userAgent);110if (msie && msie[1]) {111// IE in compatibility mode usually identifies itself as MSIE 7.0; in this112// case, use the Trident version to determine the version of IE. For more113// details, see the links above.114var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);115if (msie[1] == '7.0') {116if (tridentVersion && tridentVersion[1]) {117switch (tridentVersion[1]) {118case '4.0':119return 8;120case '5.0':121return 9;122case '6.0':123return 10;124case '7.0':125return 11;126}127} else {128return 7;129}130} else {131return Number(msie[1]);132}133}134return goog.labs.useragent.verifier.NOT_IE;135};136137138