js判断当前是ios还是android

时间:2022-12-07 17:57:59

1.  判断是ios还是android

let u = navigator.userAgent, 
let isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1; //g let isIOS = !!u.match(/(i[^;] ;( U;)? CPU. Mac OS X/); //ios终端 if (isAndroid) { //这个是安卓操作系统 } if (isIOS) {   //这个是ios操作系统 }

2.  判断是PC端还是M端

let userAgent = navigator.userAgent;
let Agents = ["Android", "iPhone",
    "SymbianOS", "Windows Phone",
    "iPad", "iPod"];
let flag = true;
for (let i in Agents) {
    if (userAgent.indexOf(Agents[i]) > -1) {
        flag = false;
        break;
    }
}
if(flag){
  //这个是PC端
}else{
  //这个是M端
}