Java爬坑--js代码判断浏览器种类IE、FF、Opera、Safari、chrome及版本

userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。

一般来讲,它是在 navigator.appCodeName 的值之后加上斜线和 navigator.appVersion 的值构成的。

例如:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)。

注:用户代理头:user-agent header。

 1 function myBrowser(){
 2     //取得浏览器的userAgent字符串
 3     var userAgent = navigator.userAgent;
 4     var isOpera = userAgent.indexOf("Opera") > -1;
 5     if (isOpera) {
 6         return "Opera"
 7     };
 8     //判断是否Opera浏览器
 9     if (userAgent.indexOf("Firefox") > -1) {
10         return "FF";
11     }
12     //判断是否Firefox浏览器
13     if (userAgent.indexOf("Chrome") > -1){
14         return "Chrome";
15     }
16     //判断是否Safari浏览器
17     if (userAgent.indexOf("Safari") > -1) {
18         return "Safari";
19     }
20     //判断是否IE浏览器
21     if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
22         return "IE";
23     };
24 }

微信存在 MicroMessenger/ NetType/ Language/
手机浏览器存在 Version/XX.XX Mobile/XX.XX
啥都不存在是WebApp,还有个 window.navigator.standalone 判断

我的微信判断

1 function is_weixin(){ 
2     var ua = window.navigator.userAgent.toLowerCase(); 
3     if(ua.match(/MicroMessenger/i) == 'micromessenger'){ 
4         return true; 
5     }else{ 
6         return false; 
7     }
8 } 

 

posted @ 2017-12-11 14:03  杨康是个大坏蛋  阅读(1494)  评论(0编辑  收藏  举报