js判断是移动端还是电脑端

function isMobile() {
const userAgent = navigator.userAgent.toLowerCase();
const mobileKeywords = [‘iphone’, ‘android’, ‘windows phone’];
for (let keyword of mobileKeywords) {
if (userAgent.indexOf(keyword) > -1) {
return true;
}
}
return false;
}

if (isMobile()) {
console.log(“这是移动端”);
} else {
console.log(“这是PC端”);
}

你可能感兴趣的:(javascript,开发语言,ecmascript)