检测微信和支付宝浏览器

 <script>
        // 判断是否是在微信端还是支付宝端浏览器打开
        // 方法一
        // var ua = navigator.userAgent.toLowerCase();
        // var isWeixin = ua.indexOf('micromessenger') != -1;
        // if (isWeixin) {
        //     console.log("微信");
        //     alert("微信");
        //     // return true;
        // } else {
        //     console.log("非微信");
        //     alert("非微信");
        //     // return false;
        // }

        //方法二
        function is_weixn() {
            // navigator.userAgent返回当前用户所使用的是什么浏览器,toLowerCase()将获得的信息变成小写
            var userAgent = navigator.userAgent.toLowerCase();

            if (userAgent.match(/MicroMessenger/i) == "micromessenger") { // 判断是微信app的浏览器
                alert("微信");
                return true;
            } else if (userAgent.match(/Alipay/i) == "alipay") { // 判断是支付宝app的浏览器
                alert("支付宝");
                return true;
            } else { // 非微信也非支付宝的浏览器
                console.log("非微信非支付宝");
                return false;
            }
        }
        is_weixn();
    </script>

猜你喜欢

转载自blog.csdn.net/yerongtao/article/details/80762379