JS_判断打开的是什么手机品牌,判断是否是手机,平板,pc

判断业务是否是 iphone、华为、小米、oppo、view、三星 打开
手机品牌userAgent库 http://www.fynas.com/ua

function judgeBrand(sUserAgent) {
	var isIphone = sUserAgent.match(/iphone/i) == 'iphone';
	var isHuawei = sUserAgent.match(/huawei/i) == 'huawei';
	var isHonor = sUserAgent.match(/honor/i) == 'honor';
	var isOppo = sUserAgent.match(/oppo/i) == 'oppo';
	var isOppoR15 = sUserAgent.match(/pacm00/i) == 'pacm00';
	var isVivo = sUserAgent.match(/vivo/i) == 'vivo';
	var isXiaomi = sUserAgent.match(/mi\s/i) == 'mi ';
	var isXiaomi2s = sUserAgent.match(/mix\s/i) == 'mix ';
	var isRedmi = sUserAgent.match(/redmi/i) == 'redmi';
	var isSamsung = sUserAgent.match(/sm-/i) == 'sm-';
	var isLG = sUserAgent.match(/lg/i) == 'lg';
	if (isIphone) {
		return 'iPhone';
	} else if (isHuawei || isHonor) {
		return 'Huawei';
	} else if (isOppo || isOppoR15) {
		return 'Oppo';
	} else if (isVivo) {
		return 'vivo';
	} else if (isXiaomi || isRedmi || isXiaomi2s) {
		return 'mi';
	} else if (isSamsung) {
		return 'Samsung';
	} else if (isLG) {
		return 'LG';
	} else {
		return '其他型号手机';
	}
}

var brand = judgeBrand(navigator.userAgent.toLowerCase());

判断是否是手机,平板,pc

function checkAgent() {
	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 (!(bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM || bIsIpad)) {
		return 'pc';
	} else if(bIsIpad){
		return 'pad';
	}else{
		return 'phone';
	}
}

注:以上functioncheckAgent,在使用华为P20 Pro 默认浏览器打开时,会被误判为PC。因为在华为P20 Pro的默认浏览器上,并没有重写userAgent,拿到的userAgent跟在PC上拿到的一样。目前只发现华为机型的默认浏览器有这个错误

你可能感兴趣的:(#,JS,javascript,智能手机,电脑)