原生h5下载app

<div class="btn">
  <a class="btn-sty a-btn" href="android" style="display:none">立即下载a>
  <a class="btn-sty i-btn" href="ios scheme://" style="display:none">立即下载a>
div>
var androidBtn = document.getElementsByClassName("a-bt")
  var iosdBtn = document.getElementsByClassName("i-bt")
  var ua = navigator.userAgent.toLowerCase()
  var isWeixin = ua.indexOf('micromessenger') != -1
  var isAndroid = ua.indexOf('android') != -1
  var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1)
  var d = new Date()
  var t0 = d.getTime()
  // 判断是安卓按钮还是ios按钮
  (function() {
    if(isAndroid) {
      androidBtn.style.display = "show"
    } else if(isIos){
      iosdBtn.style.display = "show"
    }
  })
  androidBtn.addEventListener('click',openApp('android src'))
  iosdBtn.addEventListener('click',openApp('app store src'))
  function openApp(src) {
    var delay = setInterval(() => {
      var d = new Date()
      var t1 = d.getTime()
      if (t1-t0 < 3000 && t1-t0 > 2000) {
        window.location.href = src
      }
      if (t1-t0 >= 3000) {
        clearInterval(delay)
      }
    }, 2000);
  }

你可能感兴趣的:(H5)