網上找的不夠完整,參考了下淘寶,立貼備忘。
<a href="javascript:;" id="opener" >打開客戶端</a>
<script type="text/javascript">
var timer, ifr;
document.getElementById('openApp').onclick = function(e){
// 通過iframe的方式試圖打開APP,如果能正常打開,會直接切換到APP,並自動阻止a標籤的默認行爲
ifr = document.createElement('iframe');
ifr.src = 'myapp://xxxx'; //APP定義的打開協議
ifr.style.display = 'none';
document.body.appendChild(ifr);
//1秒內未打開APP,則跳轉下載等。
timer = window.setTimeout(function(){
location.href = "http://下載鏈接/...";
}, 1000);
}
//摘自“淘寶”,打開APP後,自動移除下載跳轉
window.onblur = function(){
if(timer){
clearTimeout(timer);
timer = null;
}
if(ifr){
document.body.removeChild(ifr);
}
}
</script>
大部分代碼摘自http://blog.woodbunny.com/tb.php?sc=2aab02&id=140