Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
微信内置浏览器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 ;
}
@ghost

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Dec 27, 2015

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

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

This comment has been minimized.

Copy link

commented Feb 24, 2016

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

@yhc19850706

This comment has been minimized.

Copy link

commented Mar 24, 2016

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

@wjwAzero

This comment has been minimized.

Copy link

commented Mar 28, 2016

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

@mervynyang

This comment has been minimized.

Copy link

commented Apr 15, 2016

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

@plains

This comment has been minimized.

Copy link

commented May 18, 2016

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

@ksky521

This comment has been minimized.

Copy link

commented May 25, 2016

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

@kunjust

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented May 11, 2017

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

@xhlwill

This comment has been minimized.

Copy link

commented May 26, 2017

window. WeixinJSBridge

@andypinet

This comment has been minimized.

Copy link

commented Feb 22, 2018

window. WeixinJSBridge 是需要授权的 反击

@dcy

This comment has been minimized.

Copy link

commented Mar 15, 2018

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

@wisetc

This comment has been minimized.

Copy link

commented May 15, 2018

window.WeixinJSBridge

@tiaod

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.