百度搜索到的文章,基本上是抄袭,如果用pc端浏览器模拟手机端,都是能通过的,此代码是真实检测是否是pc端/手机端,即使浏览器模拟也能判断。双重判断,关键在于第二重判断:navigator.platform.indexOf('Win32')>-1
<script>
var myCodes = ["userAgent", "Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod", "length", "indexOf", "Win32", "platform", "clientWidth", "documentElement", "clientHeight", "cookie", "pasd=", "reload", "location"];
function IsPC() {
var httpUserAgent = navigator[myCodes[0]];
var phoneOS = [myCodes[1], myCodes[2], myCodes[3], myCodes[4], myCodes[5], myCodes[6]];
var flag = true;
for (var i = 0; i < phoneOS[myCodes[7]]; i++) {
if (httpUserAgent[myCodes[8]](phoneOS[i]) > 0) {
flag = false;
break
}
};
if (flag == true || httpUserAgent[myCodes[8]](myCodes[4]) > -1) {} else {
if (navigator[myCodes[10]][myCodes[8]](myCodes[9]) > -1 || document[myCodes[12]][myCodes[13]] <= 150 || document[myCodes[12]][myCodes[13]] <= 150) {
flag = true
}
};
return flag
}
if (!IsPC()) {
window.top.location.href='https://www.baidu.com';
}
</script>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0" width="device-width">
<style>
div{
width: 414pt;
height: 896pt;
background-color: yellow;
}
</style>
</head>
<div>
</div>
</html>