JS判断当前设备是 PC IOS Andriod

时间:2022-04-05 17:30:44

JS判断当前设备是 PC IOS Andriod

<script >

    window.onload = function(){

         var isPc = IsPC();
var isAndroid = is_android();
if (isPc){
  //pc
  location.href = url;
}else{
  if (isAndroid){
     //安卓
     location.href = "";
   }else{
     //IOS
    location.href = "";   }
} } //如果返回的是false说明当前操作系统是手机端,如果返回的是true则说明当前的操作系统是电脑端
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
       flag = false;
       break;
    }
   }
   return flag;
  } //如果返回true 则说明是Android false是ios
function is_android() {
var u = navigator.userAgent, app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid) {
  //这个是安卓操作系统
  return true;
}
if (isIOS) {
  //这个是ios操作系统
   return false;
}
} </script>