js判斷手機端還是電腦PC端(以及注意事項)


這里是vue腳手架中的例子

在router.index中聲明方法,在導航守衛中調用並跳轉
要注意跳轉的網址必須加上 http 或https
1: 不加是不會打開外網的連接,會在你服務內找localhost:XX/XX,
2:加上的話,會變成正常的地址
3:location 獲取的是瀏覽器地址欄內的地址。

function browserRedirect() {
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (
    bIsIpad ||
    bIsIphoneOs ||
    bIsMidp ||
    bIsUc7 ||
    bIsUc ||
    bIsAndroid ||
    bIsCE ||
    bIsWM
  ) {
    console.log("手機登錄");
     window.location.href ="http://mobile.yuyihui.com.cn"
  } else {
    console.log("電腦登錄,為您跳轉去電腦端吧");
  }
}

// });
router.beforeEach((to, from, next) => {
  browserRedirect();
  next()
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM