您好,欢迎访问一九零五行业门户网

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

1、第一种:
复制代码 代码如下:
function ispc() {
    var useragentinfo = navigator.useragent;
    var agents = [android, iphone,
                symbianos, windows phone,
                ipad, ipod];
    var flag = true;
    for (var v = 0; 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
   }
  }
 }
};
使用方法:
另外一篇,感觉不如上面的好,不过大家可以参考下
复制代码 代码如下:
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;
 }
}
其它类似信息

推荐信息