网页原生js判断"客户端设备类型是移动端还是PC端并跳转页面

<!DOCTYPE html><html><head><meta charset="utf-8"><title>当前客户端是否为手机</title><script>function isMobile() {var ua = navigator.userAg...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>当前客户端是否为手机</title>
<script>
function isMobile() {
var ua = navigator.userAgent.toLowerCase();
var StringPhoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i" +
"|windows (phone|ce)|blackberry" +
"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp" +
"|laystation portable)|nokia|fennec|htc[-_]" +
"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
var StringTableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser" +
"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
console.log(ua);
var isIphone = ua.match(StringPhoneReg),
isTable = ua.match(StringTableReg),
isMobile = isIphone || isTable;
if (isMobile) {
alert("yes");
window.location.href = "http://www.baidu.com";
return true;
} else {
alert("no");
window.location.href = "http://www.taobao.com";
return false;
}
}
</script>
</head>
<body onload="isMobile();">
<p> yes : 表示为移动端,然后跳转到百度页面,实际使用跳转到移动端域名 </p>
<p> no : 表示为PC端,然后跳转到淘宝页面,实际使用跳转PC域名 </p>
</body>
</html>

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

651 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章