1. 程式人生 > >微信掃一掃二維碼跳轉手機外部瀏覽器打開下載app的鏈接是怎麽實現的

微信掃一掃二維碼跳轉手機外部瀏覽器打開下載app的鏈接是怎麽實現的

按鈕 navi com itl 限制 prev back 選擇 con

由於微信的限制,應用文件在內置瀏覽器中下載全部被屏蔽掉,造成很多人用微信掃描二維碼下載時點擊下載按鈕沒反應,我想到的是做一個提示用戶在瀏覽器中打開下載。

之前寫過的兩篇文章:微信打開網址添加在瀏覽器中打開提示 和 微信掃描打開APP下載鏈接提示代碼優化,盡管已經做得很簡單,但發現很多問這類問題的都是小白。

其實原來很簡單,就是判斷當前是在微信內置瀏覽器中,然後將默認隱藏的提示層顯示出來。

技術分享圖片

第一步:判斷微信的UA。

  1. var ua = navigator.userAgent;
  2. 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;}

第四步:點擊按鈕顯示隱藏層,點擊隱藏層關閉,總的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);

以上代碼,你再也不用擔心有多個按鈕了。

微信掃一掃二維碼跳轉手機外部瀏覽器打開下載app的鏈接是怎麽實現的