[js]简单判断手持设备类型和系统

写JavaScript时,有时候需要判断手持设备的系统和类型(说白了就是手机,主要是通过浏览器的userAgent来判断,注意:userAgent可以伪造的)。

 1 var mobile = {
 2     Android: function() {
 3         return navigator.userAgent.match(/Android/i) ? true: false
 4     },
 5     BlackBerry: function() {
 6         return navigator.userAgent.match(/BlackBerry/i) ? true: false
 7     },
 8     iPhone: function() {
 9         return navigator.userAgent.match(/iPhone/i) ? true: false
10     },
11     iPad: function() {
12         return navigator.userAgent.match(/iPad/i) ? true: false
13     },
14     iPod: function() {
15         return navigator.userAgent.match(/iPod/i) ? true: false
16     },
17     iOS: function() {
18         return (mobile.iPhone() || mobile.iPad() || mobile.iPod())
19     },
20     Windows: function() {
21         return navigator.userAgent.match(/IEMobile/i) ? true: false
22     },
23     any: function() {
24         return (mobile.Android() || mobile.BlackBerry() || mobile.iOS() || mobile.Windows())
25     }
26 };

使用方法也很简单

1 if(mobile.iPad){
2     console.log("当前设备类型为:iPad")
3 }
if(mobile.Android){
    console.log("当前的操作系统为:Android");    
}else{
    console.log("不是Android系统")
}

 

posted @ 2018-01-31 02:46  munglan  阅读(356)  评论(0)    收藏  举报