获取浏览器的userAgent,这个字符串信息包含了手机使用的系统描述
// toLowerCase()转化为小写,方便后面匹配~
var phone_flag = navigator.userAgent.toLowerCase();
console.log(phone_flag)
F12打开控制台,选择手机调试模式~
可以看到,选择iphone 5/SE的时候,浏览器信息中出现'iphone'的字符串,安卓浏览器包含'android',WP浏览器包含'windows phone',可以根据这些关键字进行判断
var phone_flag = navigator.userAgent.toLowerCase();
if (phone_flag.search(/iphone/) != -1) {
console.log("iphone")
}
else if (phone_flag.search(/android/) != -1) {
console.log("android")
}
else if (phone_flag.search(/windows phone/) != -1) {
console.log("windows phone")
}