判断客户端是PC端还是移动端访问

export function getUserAgent() {
let agent = window.navigator.userAgent
let system = agent.indexOf("Mac" || "Pho" || "and" || "And")
if (system > 0) {
   //移动端进入
return "98%";
} else {
   //浏览器进入
return "50%";
}
}
其他方法
方法一 (推荐)
function IsPC(){
  var userAgentInfo = navigator.userAgent;
  var Agents = new Array("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;
}
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  //alert(navigator.userAgent);  
  window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
  //alert(navigator.userAgent); 
  window.location.href ="Android.html";
} else {
  window.location.href ="pc.html";
};

方法二

var browser={
    versions:function(){ 
           var u = navigator.userAgent, app = navigator.appVersion; 
           return {//移动终端浏览器版本信息 
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad  
                webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
                weixin: u.indexOf('MicroMessenger') > -1, //是否微信 
                qq: u.match(/\sQQ/i) == " qq" //是否QQ
            };
         }(),
         language:(navigator.browserLanguage || navigator.language).toLowerCase()
} 
 
  if(browser.versions.mobile || browser.versions.ios || browser.versions.android || 
      browser.versions.iPhone || browser.versions.iPad){      
           window.location = "http://m.zhaizhainv.com";    
  }

参考链接  https://blog.csdn.net/example440982/article/details/90905730?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-1.nonecase

上一篇:el-input 兼容ie9密码变明文


下一篇:js实现关闭当前页面