<script>
var agent = "";
function isAndroid() {
agent = navigator.userAgent.toLowerCase();
let result = {
device: function () {
if (/windows/.test(agent)) {
return 'windows pc';
} else if (/iphone|ipod/.test(agent) && /mobile/.test(agent)) {
return 'iphone';
} else if (/ipad/.test(agent) && /mobile/.test(agent)) {
return 'ipad';
} else if (/android/.test(agent) && /mobile/.test(agent)) {
return 'android';
} else if (/linux/.test(agent)) {
return 'linux pc';
} else if (/mac/.test(agent)) {
return 'mac';
} else {
return 'other';
}
}(),
};
document.getElementById("agent").innerText = agent;
document.getElementById("showAndroid").innerText = result.device;
return result.device;
}
</script>
mozilla/5.0 (macintosh; intel mac os x 10_15_6) applewebkit/537.36 (khtml, like gecko) chrome/99.0.4844.51 safari/537.36
不准确的问题原因
当安卓或则iOS设备, 在其WebView组件里面设置了userAgent, 将覆盖掉原设备的userAgent, 会导致原来的判断代码失效
|