用js判断是否是微信浏览器

判断是否是微信浏览器,网上的大部分代码是用ua判断的,这种办法容易伪造,在github上找了一个用jsbridge判断的,如下:

var isWeixin = false;

if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
    isWeixin = true;
} else {
    if (document.addEventListener) {
        document.addEventListener("WeixinJSBridgeReady", function() { isWeixin = true; }, false);
    } else if (document.attachEvent) {
        document.attachEvent("WeixinJSBridgeReady", function() { isWeixin = true; });
        document.attachEvent("onWeixinJSBridgeReady", function() { isWeixin = true; });
    }
}

你可能感兴趣的:(js)