原文链接: 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内打开)。但是在真机上是好的。