高级程序书第238~240页有详解
转载自:https://blog.csdn.net/danzhang1010/article/details/25028149
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>检测浏览器呈现引擎版本和浏览器版本</title>
<script type="text/javascript">
var client=function(){
var UA=navigator.userAgent;
document.write(UA+"<BR>");
var PF=navigator.platform;
var engines={
ie:0,
webkit:0,
khtml:0,
gecko:0,
opera:0,
ver:null
};
var browsers={
ie:0,
chrome:0,
safari:0,
konqueror:0,
firefox:0,
opera:0,
ver:null
};
var platform={
win:false,
mac:false,
linux:false,
iphone:false,
ipod:false,
ipad:false,
ios:false,
android:false,
nokiaN:false,
winMobile:false
};
if(window.opera){
browsers.ver= engines.ver=window.opera.version();
browsers.opera=engines.opera=parseFloat(engines.ver);
}else if(/AppleWebKit/(\S+)/.test(UA)){//说明是webkit引擎,使用webkit引擎的有chrome,safari,和opera
engines.ver=RegExp["$1"];
engines.webkit=parseFloat(engines.ver);
if(/OPR\/(\S+)/.test(UA)){
browsers.ver=RegExp["$1"];
browsers.opera=parseFloat(browsers.ver);
}
else if(/Version\/(\S+)/.test(UA)){
browsers.ver=RegExp["$1"];
browsers.safari=parseFloat(browsers.ver);
}else if(/Chrome\/(\S+)/.test(UA)){
browsers.ver=RegExp["$1"];
browsers.chrome=parseFloat(browsers.ver);
}
}else if(/KHTML\/(\S+)/.test(UA)||/Konqueror\/([^;]+)/.test(UA)){
browsers.ver=engines.ver=RegExp["$1"];
browsers.konqueror=engines.khtml=parseFloat(engines.ver);
}else if(/rv\:([^\)]+)\) Gecko\/\d{8}/.test(UA)){
engines.ver=RegExp["$1"];
engines.gecko=parseFloat(engines.ver);
if(/Firefox\/(\S+)/.test(UA)){
browsers.ver=RegExp["$1"];
browsers.firefox=parseFloat(browsers.ver);
}
}
else if(/Trident\/([^;]+)/.test(UA)){
browsers.ver=engines.ver=parseFloat(RegExp["$1"])+4.0;
engines.ie=browsers.ie=engines.ver;
}else if(/MSIE ([^;]+)/.test(UA)){
browsers.ver=engines.ver=RegExp["$1"];
engines.ie=browsers.ie=parseFloat(engines.ver);
}
//检测平台
platform.win=PF.indexOf("Win")==0;
platform.mac=PF.indexOf("Mac")==0;
platform.linux=(PF.indexOf("x11")==0)||(PF.indexOf("Linux")==0);
platform.iphone=UA.indexOf("iPhone")>-1;
platform.ipod=UA.indexOf("iPod")>-1;
platform.ipad=UA.indexOf("iPad")>-1;
platform.nokiaN=UA.indexOf("nokiaN")>-1;
//检测windows的版本
if(platform.win){
if(/Win(?:dows )?([^;\)]\S+)\s?(\d+\.\d+)?/.test(UA)){
if(RegExp["$1"]=="NT"){
switch(RegExp["$2"]){
case '5.0':
platform.win='2000';
break;
case '5.1':
platform.win='XP';
break;
case '6.0':
platform.win='Vista';
break;
case '6.1':
platform.win='7';
break;
default:
platform.win='NT';
break;
}
}else if(RegExp["$1"]=='9x'){
platform.win='ME';
}else{
platform.win=RegExp["$1"];
}
}
}
//检测IOS版本
if(platform.mac&&UA.indexOf('Mobile')>-1){
if(/CPU (?:iPhone)?OS (\d_\d)/.test(UA)){
platform.ios=parseFloat(RegExp.$1.replace("_","."));
}else{
platform.ios=2;
}
}
//检测android版本
if(/Android (\d+\.\d+)/.test(UA)){
platform.android=parseFloat(RegExp["$1"]);
}
return {engines:engines,browsers:browsers,platform:platform};
}();
for(var t in client.engines){
document.write("engin:"+t+":"+client.engines[t]+"
");
}
for(var i in client.browsers){
document.write("browsers:"+i+":"+client.browsers[i]+"<br>");
}
for(var tt in client.platform){
document.write("platform:"+tt+":"+client.platform[tt]+"<br>");
}
</script>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>