由於微信的限制,應用文件在內置瀏覽器中下載全部被屏蔽掉,造成很多人用微信掃描二維碼下載時點擊下載按鈕沒反應,我想到的是做一個提示用戶在瀏覽器中打開下載。
可以參考:微信打開網址添加在瀏覽器中打開提示 和 微信掃描打開APP下載鏈接提示代碼優化。
其實原來很簡單,就是判斷當前是在微信內置瀏覽器中,然后將默認隱藏的提示層顯示出來。
案例如下:
一、遮罩提示:
二、微信跳轉:微信自動跳轉手機默認瀏覽器打開下載鏈接
安卓展示:直接跳轉瀏覽器下載APK
蘋果展示:直接跳轉打開蘋果商店
部分關鍵代碼
第一步:判斷微信的UA。
var ua = navigator.userAgent; var isWeixin = !!/MicroMessenger/i.test(ua);
<a href="http://caibaojian.com/test.apk" id="JdownApp">點擊下載APP</a> <a href="http://caibaojian.com/test.apk" id="JdownApp2" class="btn-warn">點擊下載APP2</a> <div class="wxtip" id="JweixinTip"> <span class="wxtip-icon"></span> <p class="wxtip-txt">點擊右上角<br/>選擇在瀏覽器中打開</p> </div>
第三步:添加css樣式
.wxtip{background: rgba(0,0,0,0.8); text-align: center; position: fixed; left:0; top: 0; width: 100%; height: 100%; z-index: 998; display: none;}
.wxtip-icon{width: 52px; height: 67px; background: url(weixin-tip.png) no-repeat; display: block; position: absolute; right: 20px; top: 20px;}
.wxtip-txt{margin-top: 107px; color: #fff; font-size: 16px; line-height: 1.5;}
function weixinTip(ele){
var ua = navigator.userAgent;
var isWeixin = !!/MicroMessenger/i.test(ua);
if(isWeixin){
ele.onclick=function(e){
window.event? window.event.returnValue = false : e.preventDefault();
document.getElementById('JweixinTip').style.display='block';
}
document.getElementById('JweixinTip').onclick=function(){
this.style.display='none';
}
}
}
var btn1 = document.getElementById('JdownApp');//下載一
weixinTip(btn1);
var btn2 = document.getElementById('JdownApp2'); //下載二
weixinTip(btn2);
以上代碼,你再也不用擔心有多個按鈕了。

