<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body id="body">
<a href="javascript:;" id="openApp" style="font-size: 30px;">打开客户端</a>
</body>
<script type="text/javascript">
document.getElementById('openApp').onclick = function(e) {
//ios
if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
var browser = navigator.userAgent.toLowerCase();
if(browser.match(/micromessenger/i)) {
//微信内置浏览器
window.location.href ="hengtaixin://";
window.setTimeout(function() {
window.location.href = "https://itunes.apple.com/us/app/cheng-shi-gao-er-fu/id1008696844?mt=8&uo=4";
}, 1000)
return
} else {
window.location.href ="hengtaixin://"; //ios app协议,由ios同事提供
window.setTimeout(function() {
window.location.href = "https://itunes.apple.com/us/app/cheng-shi-gao-er-fu/id1008696844?mt=8&uo=4";
}, 2000)
return
}
}
if(navigator.userAgent.match(/android/i)) {
//Android
window.location.href = "app://city_golf";//安卓协议,由安卓同事提供
window.setTimeout(function() {
window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=gr eenjoy.golf.app&g_f=991653"
}, 2000)
return
}
};
</script>
</html>
上面的代码中分为ios,安卓和微信内置浏览器三种情况。实现方法均是window.location.href ="xxx://";首先尝试通过协议来唤醒手机里面的app,如果在2000ms之内跳转成功,则说明手机已经安装app,直接打开,下面定时器的代码将不会执行。如果超过2000ms之后还在这个页面,则证明用户手机没有安装app,则执行定时器里面的代码,跳到应用市场下载。
原文:https://blog.csdn.net/tiantiandashi/article/details/81074927