判断客户端设备的代码:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断客户端设备的代码:相关的知识,希望对你有一定的参考价值。

这些是我在网上找到的代码,只供自己做项目的时候参考:

第一种:

 1 function fBrowserRedirect() {
 2             var sUserAgent = navigator.userAgent.toLowerCase();
 3             var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
 4             var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
 5             var bIsMidp = sUserAgent.match(/midp/i) == "midp";
 6             var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
 7             var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
 8             var bIsandroid = sUserAgent.match(/android/i) == "android";
 9             var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
10             var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
11 
12             if (bIsIpad) {
13                 window.location.href = "http://ipad.tongzhuo100.com/";
14             }else if (bIsIphoneOs || bIsAndroid || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM) {
15                 window.location.href = "http://wap.tongzhuo100.com/";
16             }
17             else{
18                 //alert("PC/MAC/LUNIX"+sUserAgent);
19             }
20         }
21         fBrowserRedirect();

第二种百度的:

 

 1 function uaredirect(f) {
 2  try {
 3   if (document.getElementById("bdmark") != null) {
 4    return
 5   }
 6   var b = false;
 7   if (arguments[1]) {
 8    var e = window.location.host;
 9    var a = window.location.href;
10    if (isSubdomain(arguments[1], e) == 1) {
11     f = f + "/#m/" + a;
12     b = true
13    } else {
14     if (isSubdomain(arguments[1], e) == 2) {
15      f = f + "/#m/" + a;
16      b = true
17     } else {
18      f = a;
19      b = false
20     }
21    }
22   } else {
23    b = true
24   }
25   if (b) {
26    var c = window.location.hash;
27    if (!c.match("fromapp")) {
28     if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {
29      location.replace(f)
30     }
31    }
32   }
33  } catch(d) {}
34 }
35 function isSubdomain(c, d) {
36  this.getdomain = function(f) {
37   var e = f.indexOf("://");
38   if (e > 0) {
39    var h = f.substr(e + 3)
40   } else {
41    var h = f
42   }
43   var g = /^www\./;
44   if (g.test(h)) {
45    h = h.substr(4)
46   }
47   return h
48  };
49  if (c == d) {
50   return 1
51  } else {
52   var c = this.getdomain(c);
53   var b = this.getdomain(d);
54   if (c == b) {
55    return 1
56   } else {
57    c = c.replace(".", "\\.");
58    var a = new RegExp("\\." + c + "$");
59    if (b.match(a)) {
60     return 2
61    } else {
62     return 0
63    }
64   }
65  }
66 };

第三种,别人的:

 1  function getPgjs(){
 2         var agent = navigator.userAgent.toLowerCase();
 3         var res = agent.match(/android/);
 4         if(res == "android")
 5             return res;
 6         res = agent.match(/iphone/);
 7         if(res == "iphone")
 8             return "ios";
 9         res = agent.match(/ipad/);
10         if(res == "ipad")
11             return "ios";
12         res = agent.match(/windows/);
13         if(res == "windows")
14             return "wp";
15         return "pc";
16     }

就这些了,自己慢慢琢磨中

 

以上是关于判断客户端设备的代码:的主要内容,如果未能解决你的问题,请参考以下文章

设备未收到 GCM 消息

JS判断客户端是手机还是PC的2个代码

javascript常用代码片段

使用JavaScript判断用户是否为手机设备

使用JavaScript判断用户是否为手机设备

HTTP客户端代码片段