前端判别登录设备 phone or pc

1 JQUERY 引入js库

$(function(){
var MobileUA = (function() {
var ua = navigator.userAgent.toLowerCase();
var mua = {
IOS: /ipod|iphone|ipad/.test(ua),
IPHONE: /iphone/.test(ua),
IPAD: /ipad/.test(ua),
ANDROID: /android/.test(ua),
WINDOWS: /windows/.test(ua),
TOUCH_DEVICE: ('ontouchstart' in window) || /touch/.test(ua),
MOBILE: /mobile/.test(ua),
ANDROID_TABLET: false,
WINDOWS_TABLET: false,
TABLET: false,
SMART_PHONE: false
};
mua.ANDROID_TABLET = mua.ANDROID && !mua.MOBILE;
mua.WINDOWS_TABLET = mua.WINDOWS && /tablet/.test(ua);
mua.TABLET = mua.IPAD || mua.ANDROID_TABLET || mua.WINDOWS_TABLET;
mua.SMART_PHONE = mua.MOBILE && !mua.TABLET;
return mua;
}());
if (MobileUA.SMART_PHONE) {
$("#web").css('display','none');
}else{
$("#phone").css('display','none');
}
});

2.原生js

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) {
$("#web").css('display','none');
alert('phone')
} else {
$("#phone").css('display','none');
alert('window')
}
}
browserRedirect();

 

posted @ 2018-01-31 18:32  一纳子  阅读(333)  评论(0编辑  收藏  举报