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()
})