Created
March 6, 2015 03:59
-
-
Save GiaoGiaoCat/fff34c063cf0cf227d65 to your computer and use it in GitHub Desktop.
微信内置浏览器UserAgent的判断
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 检测浏览器的 User Agent 应该是非常简单的事情 | |
// 微信在 Android 下的 User Agent | |
mozilla/5.0 (linux; u; android 4.1.2; zh-cn; mi-one plus build/jzo54k) applewebkit/534.30 (khtml, like gecko) version/4.0 mobile safari/534.30 micromessenger/5.0.1.352 | |
// 微信在 iPhone 下的 User Agent | |
mozilla/5.0 (iphone; cpu iphone os 5_1_1 like mac os x) applewebkit/534.46 (khtml, like gecko) mobile/9b206 micromessenger/5.0 | |
// 通过javascript判断 | |
// 很容易看出来,微信的 User Agent 都有‘micromessenger’字符串标示,我们判断是否含有这些字符串就OK了 | |
function isWeixinBrowser(){ | |
var ua = navigator.userAgent.toLowerCase(); | |
return (/micromessenger/.test(ua)) ? true : false ; | |
} |
@andypinet 请问这意思是WeixinJSBridge可用还是不可用?
window.WeixinJSBridge
想通过window.WeixinJSBridge来判断是否为微信客户端的人参考下这个:
JS API建立在客户端浏览器内置JS对象WeixinJSBridge上。然而WeixinJSBridge并不是WebView一打开就有了,客户端需要初始化这个对象,当这个对象准备好的时候,客户端会抛出事件"WeixinJSBridgeReady"。因此,在调用WeixinJSBridge相关api时,需要做好WeixinJSBridge存在与否的判断:
if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
callback();
} else {
if (document.addEventListener) {
document.addEventListener("WeixinJSBridgeReady", callback, false);
} else if (document.attachEvent) {
document.attachEvent("WeixinJSBridgeReady", callback);
document.attachEvent("onWeixinJSBridgeReady", callback);
}
}
//callback即为调用WeixinJSBridge的相关接口的函数
出处:
https://github.com/Tencent/weui/wiki/%E5%BE%AE%E4%BF%A1JSAPI
怎么区分桌面端还是移动端呢
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
window. WeixinJSBridge 是需要授权的 反击