Home >Web Front-end >JS Tutorial >js detection browser version, core, whether it is mobile terminal example_Basic knowledge
Detect browser version, core, system and whether it is mobile
/**
* detect browser info with navigator userAgent
* @return object browser info
*/
var browser = (function(){
var userAgent = navigator.userAgent,
ua = userAgent.toLowerCase(),
browserList = {
msie : /(?:msies|trident .*rv:)([w.] )/i,
firefox : /Firefox/([w.] )/i,
chrome : /Chrome/([w.] )/i,
safari : /version/([w.] ).*Safari/i,
opera : /(?:OPR/|Opera. version/)([w.] )/i
},
kernels = {
MSIE: /(compatible;smsies|Trident/)[w.] /i,
Camino: /Camino/i,
KHTML: /KHTML/i,
Presto: /Presto/[w.] /i,
Gecko : /Gecko/[w.] /i,
WebKit: /AppleWebKit/[w.] /i
},
browser = {
kernel : 'unknow',
version : 'unknow'
}
// Detect browser
for(var i in browserList){
var matches = ua.match(browserList[i]);
browser[i] = matches ? true : false;
if(matchs){
browser.version = matches[1];
}
}
// Detection engine
for(var i in kernels){
var matches = ua.match(kernels[i]);
if(matchs){
browser.kernel = matches [0];
}
}
// System
var os = ua.match(/(WindowssNTs|MacsOSsXs|Androids|ipad.*soss|iphonesoss)([d._-] )/i);
browser.os = os!==null ? os[0] : false;
// Whether it is mobile
browser.mobile = ua.match(/Mobile/i)!==null ? true : false;
return browser;
}());