由于微信的限制,應(yīng)用文件在內(nèi)置瀏覽器中下載全部被屏蔽掉,造成很多人用微信掃描二維碼下載時(shí)點(diǎn)擊下載按鈕沒(méi)反應(yīng),我想到的是做一個(gè)提示用戶在瀏覽器中打開(kāi)下載。
可以參考:微信打開(kāi)網(wǎng)址添加在瀏覽器中打開(kāi)提示?和?微信掃描打開(kāi)APP下載鏈接提示代碼優(yōu)化。
其實(shí)原來(lái)很簡(jiǎn)單,就是判斷當(dāng)前是在微信內(nèi)置瀏覽器中,然后將默認(rèn)隱藏的提示層顯示出來(lái)。
案例如下:
一、遮罩提示:

二、微信跳轉(zhuǎn):微信自動(dòng)跳轉(zhuǎn)手機(jī)默認(rèn)瀏覽器打開(kāi)下載鏈接
安卓展示:直接跳轉(zhuǎn)瀏覽器下載APK

蘋果展示:直接跳轉(zhuǎn)打開(kāi)蘋果商店

測(cè)試體驗(yàn)地址:http://www.zjychina.cn
部分關(guān)鍵代碼
第一步:判斷微信的UA。
var?ua?= navigator.userAgent;
var?isWeixin =??!!/MicroMessenger/i.test(ua);
第二步:引入默認(rèn)隱藏層。
<a id="JdownApp">點(diǎn)擊下載APP</a>
<a id="JdownApp2" class="btn-warn">點(diǎn)擊下載APP2</a>
<div class="wxtip" id="JweixinTip">
<span class="wxtip-icon"></span>
<p class="wxtip-txt">點(diǎn)擊右上角<br/>選擇在瀏覽器中打開(kāi)</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;}
第四步:點(diǎn)擊按鈕顯示隱藏層,點(diǎn)擊隱藏層關(guān)閉,總的js代碼如下:
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);
以上代碼,你再也不用擔(dān)心有多個(gè)按鈕了。