js判断客户端是手机端还是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;
}

 

 正则简单判断:

var isMobile = /(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent);
console.log("是否是手机端", isMobile)

 

可以在开发者模式请求头里查看:NetWork -> Request Headers里查看

 

posted @ 2019-01-21 10:21  ziChin  阅读(252)  评论(0编辑  收藏  举报