js检测手机上是否有此APP,有的话打开应用,没有的话跳转到appstore

 1     //html代码中 的 a 标签,以微信为例,默认的是调用weixin scheme,去打开本机的微信,如果没有则跳转到相应连接
 2     <a href="weixin://" class="btn-download">立即打开</a>
 3   
 4     // 为btn-download 绑定事件,如果在500ms内,没有解析到协议,那么就会跳转到下载链接 
 5      var appstore, ua = navigator.userAgent;
 6         if(ua.match(/Android/i)){ 
 7             appstore = 'market://search?q=com.singtel.travelbuddy.android';
 8         }
 9         if(ua.match(/iphone|ipod|ipad/)){
10             appstore = "https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8&ign-mpt=uo%3D4"; 
11         }
12         function applink(fail){  
13             return function(){  
14                 var clickedAt = +new Date;  
15                 // During tests on 3g/3gs this timeout fires immediately if less than 500ms.  
16                 setTimeout(function(){  
17                      // To avoid failing on return to MobileSafari, ensure freshness!  
18                       if (+new Date - clickedAt < 2000){  
19                           window.location = fail;  
20                       }  
21                 }, 500);      
22             };  
23         }  
24         $('.icon-download, .btn-download')[0].onclick = applink(appstore); 

或者 IOS设备上可以

1 <meta name="apple-itunes-app" content="app-id=414478124" />

 

posted @ 2018-04-24 18:14  ming-os9  阅读(340)  评论(0编辑  收藏  举报