jquery 检测当前登陆浏览器
var userAgent = navigator.userAgent.toLowerCase(),
browser = '',
version = 0;
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
//判断是否是chrome
if($.browser.chrome){
userAgent = userAgent.substring(userAgent.indexOf('chrome/') + 7);
userAgent = userAgent.substring(0, userAgent.indexOf('.'));
version = userAgent;
// 因为这里jQuery会把chrome识别为safari,这里我们控制一下
$.browser.safari = false;
browser = 'Chrome';
}
//判断是否为Safari
if($.browser.safari){
userAgent = userAgent.substring(userAgent.indexOf('safari/') + 7);
userAgent = userAgent.substring(0, userAgent.indexOf('.'));
version = userAgent;
browser = 'Safari';
}
//判断是否为非Firefox的Mozilla浏览器
if($.browser.mozilla){
//判断是否为firefox
if(navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){
userAgent = userAgent.substring(userAgent.indexOf('firefox/') + 8);
userAgent = userAgent.substring(0, userAgent.indexOf('.'));
version = userAgent;
browser = 'Firefox';
}else{
//如果不是firefox
browser = 'Mozilla(非Firefox)';
}
}
//判断是否为Opera
if($.browser.opera){
userAgent = userAgent.substring(userAgent.indexOf('version/') + 8);
userAgent = userAgent.substring(0, userAgent.indexOf('.'));
version = userAgent;
browser = 'Opera';
}
//判断是否是IE
if($.browser.msie){
userAgent = $.browser.version;
userAgent = userAgent.substring(0, userAgent.indexOf('.'));
version = userAgent;
browser = 'IE';
}
注意本例代码支持jQuery 1.5.1~jQuery 1.8.3,1.9版本以上$.browser被废弃了
代码下载地址:下载demo