使用userAgent判断当前页面是否在webView里打开

需求背景:需要判断网页是在否在自己app里的的webView里打开(电脑端浏览器、手机qq,手机qq浏览器,微信,微信朋友圈,微博,手机safari等除外)
1、app内部:需要调用原生app方法
2、非微信的其他浏览器或webview,提示请在微信中打开页面
3、微信:发起微信授权获取用户信息。

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

猜你喜欢

转载自blog.csdn.net/u010394015/article/details/80608701