在做一个web应用的时候,经常要去判断浏览器类型,甚至如果要做多终端兼容的话,还要判断终端的系统,无论是移动端还是pc端的。

因此判断浏览器类型或者终端系统类型就显得很重要了。

我这边列了一下,我所做项目判断浏览器类型和终端系统类型的对象,可以供大家参考和交流

1.判断浏览器类型对象,版本,以及名称

 1 browser: {
 2         version: (navigator.userAgent.toLowerCase().match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, '0'])[1],
 3         chrome: /chrome/i.test(navigator.userAgent.toLowerCase()) && /webkit/i.test(navigator.userAgent.toLowerCase()) && /mozilla/i.test(navigator.userAgent.toLowerCase()),
 4         safari: /webkit/i.test(navigator.userAgent.toLowerCase()) && !this.chrome,
 5         opera: /opera/i.test(navigator.userAgent.toLowerCase()),
 6         firefox: /firefox/i.test(navigator.userAgent.toLowerCase()),
 7         ie: /msie/i.test(navigator.userAgent.toLowerCase()) && !/opera/.test(navigator.userAgent.toLowerCase()),
 8         mozilla: /mozilla/i.test(navigator.userAgent.toLowerCase()) && !/(compatible|webkit)/.test(navigator.userAgent.toLowerCase()) && !this.chrome,
 9         getBrowserName: function () {
10             if (this.chrome)return "chrome";
11             if (this.safari)return "safari";
12             if (this.opera)return "opera";
13             if (this.firefox)return "firefox";
14             if (this.ie)return "ie";
15             if (this.mozilla)return "mozilla";
16             return "unknown browser";
17         }
18     },

2.判断系统类型

 1 OS: {
 2         Windows: /win/i.test(navigator.appVersion.toLowerCase()),
 3         MacOS: /mac/i.test(navigator.appVersion.toLowerCase()),
 4         Unix: /x11/i.test(navigator.appVersion.toLowerCase()),
 5         Linux: /linux/i.test(navigator.appVersion.toLowerCase()),
 6         getOSName: function () {
 7             if (this.Windows)return "Windows";
 8             if (this.MacOS)return "MacOS";
 9             if (this.Unix)return "UNIX";
10             if (this.Linux)return "Linux";
11             return "Unknown OS";
12         }
13     },

 

posted on 2013-05-25 00:13  柯邪  阅读(309)  评论(0)    收藏  举报