如何判断浏览器是移动端还是pc端,以及是否微信端?
方法一:
// 判断浏览器函数 function isMobile(){ if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) { return true; // 移动端 }else{ return false; // PC端 } }
方法二:这个方法摘自:im.qq.com
var os = function() { var ua = navigator.userAgent, isWindowsPhone = /(?:Windows Phone)/.test(ua), isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone, isAndroid = /(?:Android)/.test(ua), isFireFox = /(?:Firefox)/.test(ua), isChrome = /(?:Chrome|CriOS)/.test(ua), isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)), isPhone = /(?:iPhone)/.test(ua) && !isTablet, isPc = !isPhone && !isAndroid && !isSymbian; return { isTablet: isTablet, isPhone: isPhone, isAndroid : isAndroid, isPc : isPc }; }(); // 使用方法 if(os.isAndroid || os.isPhone){ alert("-----"); }
判断是否为微信客户端:
var isWechart = function () { var rst = true if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)){ var ua = navigator.userAgent.toLowerCase(); if (!(ua.match(/MicroMessenger/i) == "micromessenger")) { rst = false; } }else { rst = false; } return rst; }
---------------------
原文:https://blog.csdn.net/weixin_42703239/article/details/81639197
<< 上一篇