1. 程式人生 > 其它 >h5 喚起 app(ios,and),沒下載則跳轉下載連結

h5 喚起 app(ios,and),沒下載則跳轉下載連結

技術標籤:htmlh5JS

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
    <meta charset="utf-8">
    <title>喚醒app</title>
    <div id="btn"
        style="font-size: 2.2rem;margin: 300px auto;display: block;color: #ffffff;width: 300px;height: 100px;line-height: 100px;background: rgb(213, 0, 9);text-align: center;border-radius: 15px;"
> 點選開啟</div> </head> <body> <script> function detectVersion() { let isAndroid,isIOS,isIOS9,version, u = navigator.userAgent, ua = u.toLowerCase(); if (u.indexOf('Android') > -1 || u.indexOf('Linux') >
-1) { //android終端或者uc瀏覽器 //Android系統 isAndroid = true } if(ua.indexOf("like mac os x") > 0){ //ios var regStr_saf = /os [\d._]*/gi ; var verinfo = ua.match(regStr_saf) ; version = (verinfo+
"").replace(/[^0-9|_.]/ig,"").replace(/_/ig,"."); } var version_str = version+""; if(version_str != "undefined" && version_str.length >0){ version = parseInt(version) if(version>=8){ // ios9以上 isIOS9 = true } else{ isIOS = true } } return {isAndroid,isIOS,isIOS9} } // 判斷手機上是否安裝了app,如果安裝直接開啟url,如果沒安裝,執行callback function openApp(url,callback) { let {isAndroid,isIOS,isIOS9} = detectVersion() if(isAndroid || isIOS){ var timeout, t = 4000, hasApp = true; var openScript = setTimeout(function () { if (!hasApp) { callback && callback() } document.body.removeChild(ifr); }, 5000) var t1 = Date.now(); var ifr = document.createElement("iframe"); ifr.setAttribute('src', url); ifr.setAttribute('style', 'display:none'); document.body.appendChild(ifr); timeout = setTimeout(function () { var t2 = Date.now(); if (t2 - t1 < t + 100) { hasApp = false; } }, t); } if(isIOS9){ location.href = url; setTimeout(function() { callback && callback() }, 250); setTimeout(function() { location.reload(); }, 1000); } } const btnNode = document.getElementById('btn') btnNode.addEventListener('click', () => { // 跳轉 iosApp openApp("bangbianli://www.zksr.cn?username=IM/czpDurUmGkRUIqvEh/g==",goConfirmAddr) }) // 跳轉下載 function goConfirmAddr(){ window.location.href = 'https://apps.apple.com/cn/app/%E5%B8%AE%E4%BE%BF%E5%88%A9/id1276118453' } </script> </body> </html>