<!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>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!