使用navigator.userAgent属性
userAgent: 声明了浏览器用于 HTTP 请求的用户代理头的值。
在访问页面时,设备的信息将会在此属性中显示,如这里使用电脑访问
使用iphone访问
将第一个括号中的信息提取出来就可达成目的。
// 判断是否是安卓
isAndroid() {
return navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
}
// 判断是否是苹果设备
isIos() {
return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}