Home > Web Front-end > JS Tutorial > body text

js detection browser version, core, whether it is mobile terminal example_Basic knowledge

WBOY
Release: 2016-05-16 16:51:20
Original
1023 people have browsed it

Detect browser version, core, system and whether it is mobile

Copy code The code is as follows:

/**
 * check browser version
 * @authors K
 * @date    2014-04-11 14:48:39
 * @version 1
 */

/**
 * 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;
}());

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template