判断客户端是PC还是手持设备的JS代码【转】

1、第一种:

复制代码代码如下:

function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

2、第二种:

复制代码代码如下:

function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
        window.location.href=B页面;
    }
}
browserRedirect();

百度的判断代码

复制代码代码如下:

function uaredirect(f) {
 try {
  if (document.getElementById("bdmark") != null) {
   return
  }
  var b = false;
  if (arguments[1]) {
   var e = window.location.host;
   var a = window.location.href;
   if (isSubdomain(arguments[1], e) == 1) {
    f = f + "/#m/" + a;
    b = true
   } else {
    if (isSubdomain(arguments[1], e) == 2) {
     f = f + "/#m/" + a;
     b = true
    } else {
     f = a;
     b = false
    }
   }
  } else {
   b = true
  }
  if (b) {
   var c = window.location.hash;
   if (!c.match("fromapp")) {
    if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {
     location.replace(f)
    }
   }
  }
 } catch(d) {}
}
function isSubdomain(c, d) {
 this.getdomain = function(f) {
  var e = f.indexOf("://");
  if (e > 0) {
   var h = f.substr(e + 3)
  } else {
   var h = f
  }
  var g = /^www\./;
  if (g.test(h)) {
   h = h.substr(4)
  }
  return h
 };
 if (c == d) {
  return 1
 } else {
  var c = this.getdomain(c);
  var b = this.getdomain(d);
  if (c == b) {
   return 1
  } else {
   c = c.replace(".", "\\.");
   var a = new RegExp("\\." + c + "$");
   if (b.match(a)) {
    return 2
   } else {
    return 0
   }
  }
 }
};

使用方法:
<SCRIPT type=text/javascript>uaredirect("手机站","WEB站");</SCRIPT>

另外一篇,感觉不如上面的好,不过大家可以参考下

复制代码代码如下:

var browser_class = navigator.userAgent;
var browser_class_name1 = browser_class.match("Mobile");
var browser_class_name2 = browser_class.match("mobile");
var location_url = window.location.href;
if (browser_class_name1 != null || browser_class_name2 != null) {
 if (location_url.match("wap") == null) {
  window.location.href = "http://wap.xxxx.com";
 }
} else {
 if (location_url.match("3g") != null || location_url.match("wap") != null) {
  window.location.href = "http://wap.xxxx.com";
 }
}

时间: 2024-10-09 16:58:51

判断客户端是PC还是手持设备的JS代码【转】的相关文章

js判断客户端是PC还是手持设备

1. function IsPC(){//true为pc,false为手持设备 var userAgentInfo = navigator.userAgent; var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"); var flag = true;   for 

js判断客户端是pc还是手机

function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length;

PHP判断客户端为PC还是手机

function is_mobile() { $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_pc = (strpos($agent, 'windows nt')) ? true : false; $is_mac = (strpos($agent, 'mac os')) ? true : false; $is_iphone = (strpos($agent, 'iphone')) ? true : false; $is_android

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

Javascript 判断客户端是否为 PC 还是手持设备,有时候项目中需要用到,很方便的检测,源生的哦,方法一共有两种 1.第一种: 复制代码代码如下: function IsPC() {    var userAgentInfo = navigator.userAgent;    var Agents = ["Android", "iPhone",                "SymbianOS", "Windows Phone

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

转载自:http://www.jb51.net/article/48939.htm Javascript 判断客户端是否为 PC 还是手持设备,有时候项目中需要用到,很方便的检测,源生的哦,方法一共有两种 1.第一种: function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Wi

使用JS判断客户端、浏览器、操作系统类型

一.JS判断客户端类型 JS判断客户端是否是iOS或者Android手机移动端 通过判断浏览器的userAgent,用正则来判断手机是否是ios和Android客户端. 核心代码如下: 方法一: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content=&quo

JS判断客户端是否是iOS或者Android或者ipad(三)

 *  * @function: 判断浏览器类型是否是Safari.Firefox.ie.chrome浏览器  * @return: true或false  *  */ function isSafari(){     var userAgent = navigator.userAgent.toLowerCase();     if(userAgent.indexOf("safari") > -1 && userAgent.indexOf("chrome

js 判断客户端 和 asp.net/C#判断客户端类型

1.js 判断客户端 <script language="JavaScript"> <!-- onload = function browserRedirect() { //平台.设备和操作系统 var system = { win: false, mac: false, xll: false, iphone: false }; //检测平台 var p = navigator.platform; system.iphone = p.indexOf("iPh

【JS】----判断客户端类型

JS 判断客户端是iOS还是Android 判断的逻辑是:客户端不是Android,就是iOS,就是PC,其实还有黑莓BlackBerry.塞班SymbianOS.Windows Phone等,如果需要可自行添加,我在第三条有写. 通过浏览器的 navigator.userAgent 判断是Android还是iOS: 一. 判断是Android还是iOS let u = navigator.userAgent; let isAndroid = u.indexOf('Android') > -1