Skip to content

Instantly share code, notes, and snippets.

@archangel-irk
Last active April 27, 2017 14:43
Show Gist options
  • Save archangel-irk/90bd0e0d88dc8520e169ed7141ba8f5c to your computer and use it in GitHub Desktop.
Save archangel-irk/90bd0e0d88dc8520e169ed7141ba8f5c to your computer and use it in GitHub Desktop.
Match user agent by list (compare, test, browser version support, check, get version)
// UserAgent list
// http://www.useragentstring.com/pages/useragentstring.php?name=Internet+Explorer
// User-agent string changes (Microsoft)
// https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
// Understanding user-agent strings
// https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx
// Google closure implementation
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/browser.js
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/util.js
function matchUserAgent (ua) {
var tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|edge|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return {name: 'MSIE', version: (tem[1] || '')};
}
if (M[1].toLowerCase() === 'chrome') {
tem = ua.match(/\b(OPR)\/(\d+)/);
if (tem != null) return {name: 'Opera', version: tem[2]};
tem = ua.match(/\b(Edge)\/(\d+)/);
if (tem != null) return {name: 'Edge', version: tem[2]};
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion];
tem = ua.match(/version\/(\d+)/i);
if (tem != null) M.splice(1, 1, tem[1]);
return {name: M[0], version: M[1] || 0};
}
// IE11
console.log('IE11');
console.log(matchUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'));
console.log(matchUserAgent('Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'));
// IE10.6
console.log('IE10.6');
console.log(matchUserAgent('Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0'));
// IE10
console.log('IE10');
console.log(matchUserAgent('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 7.0; InfoPath.3; .NET CLR 3.1.40767; Trident/6.0; en-IN)'));
// console.log(matchUserAgent('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)'));
// console.log(matchUserAgent('Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)'));
// IE9
console.log('IE9');
console.log(matchUserAgent('Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))'));
// IE8
console.log('IE8');
console.log(matchUserAgent('Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)'));
// IE7
console.log('IE7');
console.log(matchUserAgent('Mozilla/4.0(compatible; MSIE 7.0b; Windows NT 6.0)'));
console.log(matchUserAgent('Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)'));
// IE6
console.log('IE6');
console.log(matchUserAgent('Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)'));
console.log(matchUserAgent('Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 6.0)'));
console.log(matchUserAgent('Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; DigExt)'));
console.log(matchUserAgent('Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)'));
// OPERA 12
console.log('OPERA');
console.log(matchUserAgent('Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16'));
console.log(matchUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 OPR/44.0.2510.1449'));
// EDGE
console.log('EDGE');
console.log(matchUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'));
// Chrome
console.log('Chrome');
console.log(matchUserAgent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'));
// Firefox
console.log('Firefox');
console.log(matchUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'));
// console.log(matchUserAgent('Mozilla'));
// console.log(matchUserAgent('Mozilla'));
// console.log(matchUserAgent('Mozilla'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment