1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @fileoverview Closure user agent detection (Browser).
17 * @see <a href="http://www.useragentstring.com/">User agent strings</a>
18 * For more information on rendering engine, platform, or device see the other
19 * sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,
20 * goog.labs.userAgent.device respectively.)
24 goog.provide('goog.labs.userAgent.browser');
26 goog.require('goog.array');
27 goog.require('goog.asserts');
28 goog.require('goog.labs.userAgent.util');
29 goog.require('goog.string');
33 * @return {boolean} Whether the user's browser is Opera.
36 goog.labs.userAgent.browser.matchOpera_ = function() {
37 return goog.labs.userAgent.util.matchUserAgent('Opera') ||
38 goog.labs.userAgent.util.matchUserAgent('OPR');
43 * @return {boolean} Whether the user's browser is IE.
46 goog.labs.userAgent.browser.matchIE_ = function() {
47 return goog.labs.userAgent.util.matchUserAgent('Trident') ||
48 goog.labs.userAgent.util.matchUserAgent('MSIE');
53 * @return {boolean} Whether the user's browser is Firefox.
56 goog.labs.userAgent.browser.matchFirefox_ = function() {
57 return goog.labs.userAgent.util.matchUserAgent('Firefox');
62 * @return {boolean} Whether the user's browser is Safari.
65 goog.labs.userAgent.browser.matchSafari_ = function() {
66 return goog.labs.userAgent.util.matchUserAgent('Safari') &&
67 !goog.labs.userAgent.util.matchUserAgent('Chrome') &&
68 !goog.labs.userAgent.util.matchUserAgent('CriOS') &&
69 !goog.labs.userAgent.util.matchUserAgent('Android');
74 * @return {boolean} Whether the user's browser is Chrome.
77 goog.labs.userAgent.browser.matchChrome_ = function() {
78 return goog.labs.userAgent.util.matchUserAgent('Chrome') ||
79 goog.labs.userAgent.util.matchUserAgent('CriOS');
84 * @return {boolean} Whether the user's browser is the Android browser.
87 goog.labs.userAgent.browser.matchAndroidBrowser_ = function() {
88 return goog.labs.userAgent.util.matchUserAgent('Android') &&
89 !goog.labs.userAgent.util.matchUserAgent('Chrome') &&
90 !goog.labs.userAgent.util.matchUserAgent('CriOS');
95 * @return {boolean} Whether the user's browser is Opera.
97 goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;
101 * @return {boolean} Whether the user's browser is IE.
103 goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;
107 * @return {boolean} Whether the user's browser is Firefox.
109 goog.labs.userAgent.browser.isFirefox =
110 goog.labs.userAgent.browser.matchFirefox_;
114 * @return {boolean} Whether the user's browser is Safari.
116 goog.labs.userAgent.browser.isSafari =
117 goog.labs.userAgent.browser.matchSafari_;
121 * @return {boolean} Whether the user's browser is Chrome.
123 goog.labs.userAgent.browser.isChrome =
124 goog.labs.userAgent.browser.matchChrome_;
128 * @return {boolean} Whether the user's browser is the Android browser.
130 goog.labs.userAgent.browser.isAndroidBrowser =
131 goog.labs.userAgent.browser.matchAndroidBrowser_;
135 * For more information, see:
136 * http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html
137 * @return {boolean} Whether the user's browser is Silk.
139 goog.labs.userAgent.browser.isSilk = function() {
140 return goog.labs.userAgent.util.matchUserAgent('Silk');
145 * @return {string} The browser version or empty string if version cannot be
146 * determined. Note that for Internet Explorer, this returns the version of
147 * the browser, not the version of the rendering engine. (IE 8 in
148 * compatibility mode will return 8.0 rather than 7.0. To determine the
149 * rendering engine version, look at document.documentMode instead. See
150 * http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more
153 goog.labs.userAgent.browser.getVersion = function() {
154 var userAgentString = goog.labs.userAgent.util.getUserAgent();
155 // Special case IE since IE's version is inside the parenthesis and
157 if (goog.labs.userAgent.browser.isIE()) {
158 return goog.labs.userAgent.browser.getIEVersion_(userAgentString);
161 if (goog.labs.userAgent.browser.isOpera()) {
162 return goog.labs.userAgent.browser.getOperaVersion_(userAgentString);
166 goog.labs.userAgent.util.extractVersionTuples(userAgentString);
167 return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);
172 * @param {string|number} version The version to check.
173 * @return {boolean} Whether the browser version is higher or the same as the
176 goog.labs.userAgent.browser.isVersionOrHigher = function(version) {
177 return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),
183 * Determines IE version. More information:
184 * http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString
185 * http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
186 * http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx
187 * http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx
189 * @param {string} userAgent the User-Agent.
193 goog.labs.userAgent.browser.getIEVersion_ = function(userAgent) {
194 // IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
196 // Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
198 // See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.
199 var rv = /rv: *([\d\.]*)/.exec(userAgent);
205 var msie = /MSIE +([\d\.]+)/.exec(userAgent);
206 if (msie && msie[1]) {
207 // IE in compatibility mode usually identifies itself as MSIE 7.0; in this
208 // case, use the Trident version to determine the version of IE. For more
209 // details, see the links above.
210 var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
211 if (msie[1] == '7.0') {
212 if (tridentVersion && tridentVersion[1]) {
213 switch (tridentVersion[1]) {
239 * Determines Opera version. More information:
240 * http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond
242 * @param {string} userAgent The User-Agent.
246 goog.labs.userAgent.browser.getOperaVersion_ = function(userAgent) {
248 goog.labs.userAgent.util.extractVersionTuples(userAgent);
249 var lastTuple = goog.array.peek(versionTuples);
250 if (lastTuple[0] == 'OPR' && lastTuple[1]) {
254 return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);
259 * Nearly all User-Agents start with Mozilla/N.0. This looks at the second tuple
260 * for the actual browser version number.
261 * @param {!Array.<!Array.<string>>} versionTuples
262 * @return {string} The version or empty string if it cannot be determined.
265 goog.labs.userAgent.browser.getVersionFromTuples_ = function(versionTuples) {
266 // versionTuples[2] (The first X/Y tuple after the parenthesis) contains the
267 // browser version number.
268 goog.asserts.assert(versionTuples.length > 2,
269 'Couldn\'t extract version tuple from user agent string');
270 return versionTuples[2] && versionTuples[2][1] ? versionTuples[2][1] : '';