判断用户是pc访问还是手机访问

时间:2022-03-04 01:22:45
判断用户是pc访问还是手机访问

前天因为手机页面中点击无法链接53客服,就研究了下怎样通过js判断,用户是拿手机(ipad)访问,还是pc端访问,

如果是手机访问的话只需把链接通过attr修改a的链接地址~

<a class="c_click" onclick="fixed();" href="javascript:void(0);">aaa</a>

首先判断是否用户是拿手机还是电脑访问:

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) {

alert(“手机访问”)//判断出是手机访问

//只需要把href链接变换一下

jQuery(".c_click").attr("href","http://AA.com");

} else {
alert(“电脑访问”)

jQuery(".c_click").attr("href","javascript:void(0);")

}

}

browserRedirect()