코드 복사코드는 다음과 같습니다.
var client = function(){
//렌더링 엔진
var 엔진 = {
ie: 0,
gecko: 0,
webkit: 0,
khtml: 0,
opera: 0,
//완전 버전
ver: null
};
//browsers
var browser = {
//browsers
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
Opera: 0,
chrome: 0,
//특정 버전
ver: null
};
//platform/device/OS
var system = {
win: false,
mac: false,
x11: false,
//모바일 장치
iphone: false,
ipod: false,
ipad: false,
ios: false,
android: false,
nokiaN: false,
winMobile: false,
//game 시스템
wii: false,
ps: false
};
//렌더링 엔진/브라우저 감지
var ua = navigator.userAgent;
if (window.opera){
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera =parseFloat(engine.ver);
} else if (/AppleWebKit/(S )/.test(ua)){
engine.ver = RegExp["$1"];
engine.webkit=parseFloat(engine.ver);
//Chrome인지 Safari인지 확인
if (/Chrome/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.chrome=parseFloat(browser.ver);
} else if (/Version/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.safari=parseFloat(browser.ver);
} else {
//대략적인 버전
var safariVersion = 1;
if (engine.webkit < 100){
safariVersion = 1;
} else if (engine.webkit < 312){
safariVersion = 1.2;
} else if (engine.webkit < 412){
safariVersion = 1.3;
} else {
safariVersion = 2;
}
browser.safari = browser.ver = safariVersion;
}
} else if (/KHTML/(S )/.test(ua) || /Konqueror/([^;] )/.test(ua)){
engine.ver = 브라우저 .ver = RegExp["$1"];
engine.khtml = browser.konq =parseFloat(engine.ver);
} else if (/rv:([^)] )) Gecko/d{8}/.test(ua)){
engine.ver = RegExp["$1"];
engine.gecko=parseFloat(engine.ver);
//Firefox인지 확인
if (/Firefox/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.firefox=parseFloat(browser.ver);
}
} else if (/MSIE ([^;] )/.test(ua)){
engine.ver = browser.ver = RegExp["$1"];
engine.ie = browser.ie =parseFloat(engine.ver);
}
//브라우저 감지
browser.ie = 엔진.ie;
browser.opera = 엔진.오페라;
//플랫폼 감지
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("맥") == 0;
system.x11 = (p == "X11") || (p.indexOf("리눅스") == 0);
//Windows 운영 체제 감지
if (system.win){
if (/Win(?:dows )?([^do]{2})s?(d .d )?/ .test(ua)){
if (RegExp["$1"] == "NT"){
switch(RegExp["$2"]){
case "5.0":
system .win = "2000";
휴식;
케이스 "5.1":
system.win = "XP";
휴식;
케이스 "6.0":
system.win = "비스타";
휴식;
케이스 "6.1":
system.win = "7";
휴식;
기본값:
system.win = "NT";
휴식;
}
} else if (RegExp["$1"] == "9x"){
system.win = "ME";
} else {
system.win = RegExp["$1"];
}
}
}
//모바일 기기
system.iphone = ua.indexOf("iPhone") > -1;
system.ipod = ua.indexOf("iPod") > -1;
system.ipad = ua.indexOf("iPad") > -1;
system.nokiaN = ua.indexOf("NokiaN") > -1;
//windows 모바일
if (system.win == "CE"){
system.winMobile = system.win;
} else if (system.win == "Ph"){
if(/Windows Phone OS (d .d )/.test(ua)){;
system.win = "전화";
system.winMobile =parseFloat(RegExp["$1"]);
}
}
//iOS 버전 확인
if (system.mac && ua.indexOf("Mobile") > -1){
if (/CPU (?:iPhone )?OS (d _d )/.test(ua)){
system.ios =parseFloat(RegExp.$1.replace("_", "."));
} else {
system.ios = 2; //실제로 감지할 수 없음 - 추측
}
}
//Android 버전 확인
if (/Android (d .d )/.test(ua)){
system .android = parsFloat(RegExp.$1);
}
//게임 시스템
system.wii = ua.indexOf("Wii") > -1;
system.ps = /playstation/i.test(ua);
//반환
return {
엔진: 엔진,
브라우저: 브라우저,
시스템: 시스템
};
}();
判断浏览器의 이름称, 版本号,操work系统
var BrowserDetect = {
init: function () {
this.browser = this.searchString(this.dataBrowser) || “未知的浏览器”;
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion)
|| “未知版本”;
this.OS = this.searchString(this.dataOS) || “未知的操作系统”;
},
searchString: 函数 (数据) {
for (var i=0;ivar dataString = data[i].string;
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch ||数据[i].身份;
if (dataString) {
if (dataString.indexOf(data[i].subString) != -1)
return data[i].identity;
}
else if (dataProp)
返回 data[i].identity;
}
},
searchVersion: function (dataString) {
var index = dataString.indexOf(this.versionSearchString);
if(索引==-1)返回;
return parseFloat(dataString.substring(index this.versionSearchString.length 1));
},
dataBrowser: [
{
字符串: navigator.userAgent,
subString: "Chrome",
身份: "Chrome"
},
{ string: navigator.userAgent,
subString: "OmniWeb",
versionSearch: "OmniWeb/",
identity: "OmniWeb"
},
{
string: navigator.vendor ,
subString: "Apple",
identity: "Safari",
versionSearch: "版本"
},
{
prop: window.opera,
identity: "Opera",
versionSearch: "版本"
},
{
字符串: navigator.vendor,
subString: "iCab",
身份: "iCab"
},
{
字符串: navigator.vendor,
子字符串: "KDE",
身份: "Konqueror"
},
{
字符串: navigator.userAgent,
子字符串:“Firefox”,
身份:“Firefox”
},
{
字符串:navigator.vendor,
子字符串:“卡米诺”,
身份:“ Camino"
},
{ // 对于较新的 Netscape (6 )
string: navigator.userAgent,
subString: "Netscape",
identity: "Netscape"
},
{
字符串:navigator.userAgent,
子字符串:“MSIE”,
身份:“Internet Explorer”,
versionSearch:“MSIE”
},
{
string: navigator.userAgent,
subString: "Gecko",
identity: "Mozilla",
versionSearch: "rv"
},
{ // 对于较旧的 Netscape (4 -)
字符串:navigator.userAgent,
子字符串:“Mozilla”,
身份:“Netscape”,
版本搜索:“Mozilla”
}
],
dataOS : [
{
字符串: navigator.platform,
subString: "Win",
身份: "Windows"
},
{
字符串: navigator.platform,
subString:“Mac”,
身份:“Mac”
},
{
字符串:navigator.userAgent,
subString:“iPhone”,
身份:“ iPhone/iPod"
},
{
字符串: navigator.platform,
子字符串: "Linux",
身份: "Linux"
}
]
};
BrowserDetect.init();
alert(BrowserDetect.browser); //名称
alert(BrowserDetect.version); //版本
alert(BrowserDetect.OS) //系统