Javascript检测浏览器useAgent

近日,在学习javascript过程中,碰到需要检测浏览器类型以及版本,一番研究,遂记录下来:

1.IE浏览器

ie11                mozilla/5.0 (windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; rv:11.0) like gecko
IE10   mozilla/5.0 (compatible; msie 10.0; windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729)
ie9   mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729)
ie8   mozilla/4.0 (compatible; msie 8.0; windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729)
ie7   mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729)

检测IE10之前的版本,可以用msie,检测IE11可以用rv:11.0

2.谷歌、火狐、opera、safari

FF     mozilla/5.0 (windows nt 6.1; rv:18.0) gecko/20100101 firefox/18.0
opera mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/43.0.2357.130 safari/537.36 opr/30.0.1835.125
chro   mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/44.0.2403.125 safari/537.36
safar mozilla/5.0 (windows nt 6.1) applewebkit/534.57.2 (khtml, like gecko) version/5.1.7 safari/534.57.2

检测火狐浏览器,可以用firefox关键字
检测opera浏览器,可以用opr
检测safari浏览器,可以用只包含safari浏览器
检测谷歌浏览器,则需要用排除法来验证
3.国产浏览器:360、搜狗、QQ、世界之窗等

360 mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/31.0.1650.63 safari/537.36
mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729) 
搜狗         mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/35.0.1916.153 safari/537.36 se 2.x metasr 1.0
                mozilla/5.0 (windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; se 2.x metasr 1.0; rv:11.0) like gecko
世界之窗 mozilla/5.0 (windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; rv:11.0) like gecko
QQ浏览器 mozilla/5.0 (windows nt 6.1; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; qqbrowser/9.0.2315.400; rv:11.0) like gecko 
  360:360有两种模式,一种是谷歌浏览器模式,一种是IE浏览器模式,IE浏览器模式下,使用的是IE7.
    搜狗:两种模式,一种谷歌模式,一种是IE模式,IE模式下,使用的是IE11
    世界之窗:IE11
  QQ浏览器:IE11.
 国产浏览器都没有自己的内核,以谷歌内核或IE内核为主,因此在兼容性方面,只需兼容IE或谷歌即可。

4.源代码:

 * 函数名:Getbrowerinfo()
 * 作用:返回当前浏览器的信息
 * 输入参数:无
 * 输出参数:1.ua,返回浏览器的标识
 * 			2.pingtai,返回当前浏览器所使用的平台,有:SymbianOS、ipad、ipone、android、WP、PC等几种类型
 * 			3.leixing,返回当前浏览器类型,如:IE,firefox等
 * 			4.verson,返回当前浏览器版本号
 */
function Getbrowerinfo(){
	 this.ua = navigator.userAgent.toLowerCase();
	 //塞班平台 
	 if (this.ua.match(/(SymbianOS)/)!=null) {
		 	this.pingtai='SymbianOS';
		 	this.leixing='SymbianOS';
		 	this.version=this.ua.match(/(SymbianOS)([/])([\w.]+)/)[3];
		 };
	 if (this.ua.match(/(mobile)/i)!=null) {
	 	//安卓、ios、WP端
	 	this.pingtai='yidong';
	 	if (this.ua.match(/(ipad)/)!=null) {
		 	this.pingtai='ipad';
		 	this.leixing='ipad';
		 	this.version=this.ua.match(/(cpu os) ([\w.]+)/)[2];
		 };
		 if (this.ua.match(/(iphone)/)!=null) {
		 	this.pingtai='iphone';
		 	this.leixing='iphone';
		 	this.version=this.ua.match(/(iphone os) ([\w.]+)/)[2];
		 };
		 if (this.ua.match(/(android)/)!=null) {
		 	this.pingtai='android';
		 	this.leixing='android';
		 	this.version=this.ua.match(/(android) ([\w.]+)/)[2];
		 };
		 if (this.ua.match(/( Windows Phone OS )/)!=null) {
		 	this.pingtai='WP';
		 	this.leixing='WP';
		 	this.version=this.ua.match(/( Windows Phone OS )([\d.]+)/)[2];
	   	}; 
	 }else {
	 	//PC端,windows、linux、MAC
	 	this.pingtai='PC';
	 	//ie10以下
	 	 if (this.ua.match(/(msie) ([\w.]+)/)!=null) {
	 	 	this.leixing='IE';
		 	this.version=this.ua.match(/(msie) ([\w.]+)/)[2];
		 };
		 //ie11
		 if (this.ua.match(/(rv)([:])([\d.]+)/)!=null) {
		 	this.leixing='IE';
		 	this.version=this.ua.match(/(rv)([:])([\d.]+)/)[3];
		 };
		 //firefox
		 if (this.ua.match(/(firefox)([/])([\d.]+)/)!=null) {
		 	this.leixing='Firefox';
		 	this.version=this.ua.match(/(firefox)([/])([\d.]+)/)[3];
		 };
		 //opera
		 if (this.ua.match(/(opr)([/])([\d.]+)/)!=null) {
		 	this.leixing='Opera';
		 	this.version=this.ua.match(/(opr)([/])([\d.]+)/)[3];
		 } else{
		 	 //chrome
		 	if (this.ua.match(/(chrome)([/])([\d.]+)/)!=null) {
		 		this.leixing='Chrome';
		 		this.version=this.ua.match(/(chrome)([/])([\d.]+)/)[3];
			 } else{
			 	//safari
			 	 if (this.ua.match(/(safari)([/])([\d.]+)/)!=null) {
			 	 	this.leixing='Safari';
		 				this.version=this.ua.match(/(safari)([/])([\d.]+)/)[3];
		 			};
			 }
		 };	 
	 }
	 
	 
     
	 
	 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值