PC端项目和移动端项目之间,根据设备类型判断相互跳转

发布时间 2023-09-04 11:05:59作者: huihuihero

移动端项目配置:在index.html里添加以下代码即可

<script>
    let sUserAgent = navigator.userAgent.toLowerCase();
    let isIpad = sUserAgent.match(/ipad/i) == "ipad";
    let isIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    let isMidp = sUserAgent.match(/midp/i) == "midp";
    let isUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    let isUc = sUserAgent.match(/ucweb/i) == "ucweb";
    let isAndroid = sUserAgent.match(/android/i) == "android";
    let isCE = sUserAgent.match(/windows ce/i) == "windows ce";
    let isWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    let isMobile = isIpad || isIphoneOs || isMidp || isUc7 || isUc || isAndroid || isCE || isWM
    if (!isMobile) {  // 跳转至PC端地址
      const mSign = "m.xxxx.com"
      const pcSign = "www.xxxx.com"
      if (window.location.href.includes(mSign)) {
        window.location.href = window.location.href.replace(mSign, pcSign)
      }
    }
  </script>

PC端项目配置

将上述代码里的 if (!isMobile) { 改为 if (isMobile) { 即可