Skip to content

Instantly share code, notes, and snippets.

@GiaoGiaoCat
Created March 6, 2015 03:59
Show Gist options
  • Save GiaoGiaoCat/fff34c063cf0cf227d65 to your computer and use it in GitHub Desktop.
Save GiaoGiaoCat/fff34c063cf0cf227d65 to your computer and use it in GitHub Desktop.
微信内置浏览器UserAgent的判断
// 检测浏览器的 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 ;
}
@plains
Copy link

plains commented May 18, 2016

function isWeixinBrowser(){ return /micromessenger/i.test(navigator.userAgent); }

@ksky521
Copy link

ksky521 commented May 25, 2016

@yhc19850706 win phone版本没法修改userAgent,目前通过ua判断无解,不知道能不能通过微信框架里面特殊的全局变量

@kunjust
Copy link

kunjust commented Mar 14, 2017

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.107 Safari/534.13
微信2017.3.1升级WKWebview内核。如何识别呢?

@xigua1994
Copy link

在QQ浏览器中和在微信中识别一样

@xhlwill
Copy link

xhlwill commented May 26, 2017

window. WeixinJSBridge

@andypinet
Copy link

window. WeixinJSBridge 是需要授权的 反击

@dcy
Copy link

dcy commented Mar 15, 2018

@andypinet 请问这意思是WeixinJSBridge可用还是不可用?

@wisetc
Copy link

wisetc commented May 15, 2018

window.WeixinJSBridge

@tiaod
Copy link

tiaod commented Jun 11, 2018

想通过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

@wangfpp
Copy link

wangfpp commented Nov 5, 2024

怎么区分桌面端还是移动端呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment