求问各位大虾如何使用JavaScript判断浏览器的类型及其版本,网上搜了一堆都不怎么好用的样子
你用jquery吧
你要是用原生的javascript 要写很多代码的 根据抓到的浏览器特征去判断。
类似这样的:
<html>
<head>
<meta http-equiv="content-Type" content="text/html;charset=gb2312">
<meta name="keywords" content="站长,网页特效,网页特效代码,js特效,js脚本,脚本,广告代码,zzjs,zzjs.net,www.zzjs.net,站长特效 网" />
<meta name="description" content="www.zzjs.net,站长特效网,站长必备js特效及广告代码。大量高质量js特效,提供高质量广告代码下载,尽在站长特效网" />
<title>网页特效 检测访客浏览器信息js脚本 站长特效网欢迎您。</title>
<script type="text/javascript">
<!--
function TClientCheck() {
this.userAgent=navigator.userAgent;
this.getBrowse=TClientCheck_Browse;
this.getOS=TClientCheck_OS;
this.browseKernel="unknown";
this.isDOMSupport=false;//是否支持DOM
if(document.getElementById) {
this.isDOMSupport=true;
}
}
function TClientCheck_Browse() {
var sUA=this.userAgent;
//检测IE浏览器
if ((navigator.appName == "Microsoft Internet Explorer")) {
//检测模拟IE浏览的OPERA。edit at 2006-11-08(ver 0.1.2)
if (sUA.indexOf('Opera')!=-1) {
this.browseKernel='Presto';
if(window.opera && document.childNodes ) {
return 'Opera 7+';
} else {
return 'Opera 6-';
}
}
this.browseKernel='Trident';
if(sUA.indexOf('Maxthon')!=-1) {
return 'Maxthon';
}
if(sUA.indexOf('TencentTraveler')!=-1) { //ver 0.1.3
return '腾迅TT';
}
if(document.getElementById) {
return "IE5+";
} else {
return "IE4-";
}
}
//检测Gecko浏览器
if(sUA.indexOf('Gecko')!=-1) {
this.browseKernel='Gecko';
if(navigator.vendor=="Mozilla") {return "Mozilla";}
if(navigator.vendor=="Firebird") {return "Firebird"; }
if (sUA.indexOf('Firefox')!=-1) {return 'Firefox'; }
return "Gecko";
}
//Netscape浏览器
if(sUA.indexOf('Netscape')!=-1) {
this.browseKernel='Gecko';
if(document.getElementById) {
return "Netscape 6+";
} else {
return 'Netscape 5-';
}
}
//检测Safari浏览器
if(sUA.indexOf('Safari') != -1) {this.browseKernel='KHTML';return 'Safari';}
if(sUA.indexOf('konqueror')!=-1) {this.browseKernel='KHTML';return 'Konqueror';}
//目前世界公认浏览网页速度最快的浏览器,但它占用的系统资源也很大。
if(sUA.indexOf('Opera') != -1) {
this.browseKernel='Presto';
if(window.opera && document.childNodes ) {
return 'Opera 7+';
} else {
return 'Opera 6-';
}
return 'Opera';
}
if((sUA.indexOf( 'hotjava' )!=-1) && typeof( navigator.accentColorName ) == 'undefined' ) {return 'HotJava';}
if( document.all && document.getElementById && navigator.savePreferences && (sUA.indexOf( 'netfront' ) < 0 ) && navigator.appName != 'Blazer' ) {return 'Escape 5'; }
//Konqueror / Safari / OmniWeb 4.5+
if( navigator.vendor == 'KDE' || ( document.childNodes && ( !document.all || navigator.accentColorName ) && !navigator.taintEnabled ) ) {this.browseKernel='KHTML';return 'KDE';}
if( navigator.__ice_version ) { return 'ICEbrowser';}
if( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) {
if( document.createElement ) {
return 'iCab 3+';
} else {
return 'iCab 2-';
}
}
if(document.layers && !document.classes ) {return 'Omniweb 4.2-';}
if(document.layers && !navigator.mimeTypes['*'] ) {return 'Escape 4';}
if(navigator.appName.indexOf( 'WebTV' ) + 1 ) {return 'WebTV';}
if(sUA.indexOf( 'netgem' )!=-1 ) {return 'Netgem NetBox';}
if(sUA.indexOf( 'opentv' )!=-1 ) {return 'OpenTV';}
if(sUA.indexOf( 'ipanel' )!=-1) {return 'iPanel MicroBrowser';}
if(document.getElementById && !document.childNodes) {return 'Clue browser';}
if(document.getElementById && ( (sUA.indexOf( 'netfront' ) !=-1) || navigator.appName == 'Blazer' ) ) {return 'NetFront 3+';}
if((sUA.indexOf( 'msie' ) + 1 ) && window.ActiveXObject ) {return 'Pocket Internet Explorer'; }
return "Unknown";
}
* 操作系统信息检测
*
* @类型:公共方法
* @参数:无
* @返回:操作系统名称
* @备注:
*/
function TClientCheck_OS() {
var sUA=this.userAgent.toLowerCase();
if(sUA.indexOf( 'win' ) !=-1){
if(sUA.indexOf("nt 5.2")!=-1) {return "Windows 2003";}
if((sUA.indexOf("nt 5.1")!=-1)||(sUA.indexOf("XP")!=-1)) {return "Windows XP"; }
if((sUA.indexOf('nt 5.0')!=-1) || (sUA.indexOf('2000')!=-1)) {return 'Windows 2000';}
if((sUA.indexOf("winnt")!=-1) || (sUA.indexOf("windows nt")!=-1)) {return "Windows NT";}
if((sUA.indexOf("win98")!=-1) || (sUA.indexOf("windows 98")!=-1)) {return "Windows 98";}
return "Windows";
}
if(sUA.indexOf('linux')!=-1) {return 'Linux';}
if(sUA.indexOf("freebsd")!=-1) {return 'FreeBSD';}
if(sUA.indexOf( 'x11' )!=-1) {return 'Unix';}
if(sUA.indexOf('mac') != -1) {return "Mac"; }
if(sUA.indexOf("sunos")!=-1) {return 'Sun OS';}
if((sUA.indexOf("os/2")!=-1) || (navigator.appVersion.indexOf("OS/2")!=-1) || (sUA.indexOf("ibm-webexplorer")!=-1)) {return "OS 2"}
if(navigator.platform == 'PalmOS' ) {return 'Palm OS'; }
if((navigator.platform == 'WinCE' ) || ( navigator.platform == 'Windows CE' ) || ( navigator.platform == 'Pocket PC' ) ) {return 'Windows CE';}
if(sUA.indexOf( 'webtv')!=-1) {return 'WebTV Platform'; }
if(sUA.indexOf( 'netgem')!=-1) {return 'Netgem';}
if(sUA.indexOf( 'opentv')!=-1) {return 'OpenTV Platform';}
if(sUA.indexOf( 'symbian')!=-1) {return 'Symbian';}
return "Unknown";
}
//-->
</script>
</head>
<body>
<a href="http://www.zzjs.net/">站长特效网</a>,站长必备的高质量网页特效和广告代码。zzjs.net,站长js特效。<hr>
<!--欢迎来到站长特效网,我们网站收集大量高质量js特效,提供许多广告代码下载,网址:www.zzjs.net,zzjs@msn.com,用.net打造靓站-->
<div id="www_zzjs_net"></div>
<script type="text/javascript">
<!--
var oCC=new TClientCheck();
var oInfo=document.getElementById('www_zzjs_net');
oInfo.innerHTML='<p>您使用的是浏览器是:<strong>'+oCC.getBrowse()+'</strong></p><p>您使用的浏览器的内核是:<strong>'+oCC.browseKernel+'</strong></p><p>您使用的操作系统是:<strong>'+oCC.getOS()+'</strong></p><p>User Agent:'+oCC.userAgent+'</p>';
//-->
</script>
</body>
</html>
辛苦
@Williame: 网上复制的, 哈哈^~^ 不过以前我自己也这样写过。
用jquery吧你用userAgent来判断,navigator.userAgent.toLowerCase()
谢谢
@Williame: 不客气加关注吧 大家互相学习