逍遥云天 H5外部浏览器直接调起微信——通过url协议 weixin:// 判断是否安装微信及启动微信

h5分享到微信,h5使用微信支付这些功能,都需要先判断是否安装微信客户端,如果已安装就启动微信,如果没有安装微信,就提示用户前去安装。

我们可以通过访问微信提供的URL协议(weixin://)来实现这个功能,代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
        <title>判断手机是否安装微信</title>
    </head>
    <body>
    <a href="javascript:testApp('weixin://')" class="dl-btn" id="download">打开微信</a> 
    <script>
    function testApp(url) { 
          var timeout, t = 1000, hasApp = true; 
          setTimeout(function () { 
            if (!hasApp) { 
                //没有安装微信
                var r=confirm("您没有安装微信,请先安装微信!");
                if (r==true){
                    location.href="http://weixin.qq.com/"
                }
            }else{
                //安装微信
            }
            document.body.removeChild(ifr); 
          }, 2000) 
          
          var t1 = Date.now(); 
          var ifr = document.createElement("iframe"); 
          ifr.setAttribute('src', url); 
          ifr.setAttribute('style', 'display:none'); 
          document.body.appendChild(ifr); 
          timeout = setTimeout(function () { 
             var t2 = Date.now(); 
             if (!t1 || t2 - t1 < t + 100) { 
               hasApp = false; 
             } 
          }, t); 
        } 
    </script>
    </body>
</html>

 

posted @ 2018-11-26 18:15  二月花开  阅读(372)  评论(0编辑  收藏  举报