原文鏈接: https://blog.csdn.net/u010394015/article/details/80608701
function openInWebview () { var ua = navigator.userAgent.toLowerCase() if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信瀏覽器判斷
return false } else if (ua.match(/QQ/i) == 'qq') { // QQ瀏覽器判斷
return false } else if (ua.match(/WeiBo/i) == "weibo") { return false } else { if (ua.match(/Android/i) != null) { return ua.match(/browser/i) == null } else if (ua.match(/iPhone/i) != null) { return ua.match(/safari/i) == null } else { return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null) } } } // 使用方式
if (openInWebview()) { // 在app內打開 // to do something
} else { // 其他地方 // 發起微信授權
}
備注:此判斷在電腦端,使用手機模式預覽,且選擇安卓手機機型的時候,判斷會有問題(會判斷為是在app內打開)。但是在真機上是好的。