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

移动端项目配置:在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端项目配置:在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) {  // 跳转至移动端地址
      const mSign = "<%= VUE_APP_WEBSITE_M_SIGN %>"
      const pcSign = "<%= VUE_APP_WEBSITE_PC_SIGN %>"
      if (window.location.href.includes(pcSign)) {
        window.location.href = window.location.href.replace(pcSign, mSign)
      }
    }
  </script>
posted @ 2023-09-04 11:05  huihuihero  阅读(76)  评论(0编辑  收藏  举报