近日,在学习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
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
* 函数名: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];
};
}
};
}
}