判断是否为手机端页面/pc端的方法

判断是否为手机端:

思路:手机端跳转到一个页面,电脑端显示另外一个页面

if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = "https://www.baidu.com/";
} else {
    window.location.href = "http://news.baidu.com/";
}
方法二:正则 和 三元运算符

window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" :  "http://news.baidu.com/";

猜你喜欢

转载自blog.csdn.net/weixin_39100915/article/details/80840584
今日推荐