Skip to content

Instantly share code, notes, and snippets.

@GiaoGiaoCat
Created March 6, 2015 03:59
Show Gist options
  • Star 69 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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 ;
}
Copy link

ghost commented Oct 27, 2015

This part requires a bit more thought because it’s easier to choose a theme now than to update your theme later. When you’ve looked at some of the options at the top of the page and ,,

@cos800
Copy link

cos800 commented Dec 27, 2015

代码有点多余,test 本来就返回 true 或 false

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

@cssconfcn
Copy link

或者:
var isWeixinBrowser = (/micromessenger/i).test(navigator.userAgent);

@yhc19850706
Copy link

是否考虑过windows phone 版微信,wp中的useragent无micromessenger

@wjwAzero
Copy link

楼上的朋友,还是请先看看 wp 的占有率吧。不是针对谁。

@yy-dev7
Copy link

yy-dev7 commented Apr 15, 2016

支付宝 微信都放弃wp了 我们完全没必要考虑

@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

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