判断移动还是PC 以及微信环境

//判断pc还是移动端

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;
}

// 判断是否是微信

    function isFromWeiXin () {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        }
    }
 
// 检测是否移动端
if(/iphone|ios|android|mini|mobile|mobi|Nokia|Symbian|iPod|iPad|Windows\s+Phone|MQQBrowser|wp7|wp8|UCBrowser7|UCWEB|360\s+Aphone\s+Browser|blackberry/i.test(navigator.userAgent)) {
    location.href = '移动端地址';
}
// 检测是否移动端
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = "移动端地址"+location.search;
}
//检测是否PC端
if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = "PC端地址"+location.search;
}

// 判断 移动环境 PC环境 微信环境
function judgePcOrAppOrWx() {
let environment;
let flag=true;
let userAgentInfo = navigator.userAgent;
let userAgentInfoLower = navigator.userAgent.toLowerCase();

let agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
for (let i = 0; i <agents.length; i++) {
if (userAgentInfo.indexOf(agents[i]) > -1) {
flag =false;
break;

}
}

if (flag === true) {
environment = 'PC';
} else if (userAgentInfoLower.match(/MicroMessenger/i) != 'micromessenger') {
environment = 'APP';
} else {
environment = 'WX';
}
return environment;
},
 

posted on 2020-10-14 11:34  ranyonsue  阅读(692)  评论(0编辑  收藏  举报

导航