方法1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
toDownload();
// 新的方法
function toDownload(){
var u = navigator.userAgent;
var ua = u.toLowerCase();
if (/iphone|ipad|ipod/.test(ua)) { // iOS 系统 -> 跳AppStore下载地址
alert("iOS");
//window.location.href = 'https://itunes.apple.com/cn/app/xxxxxxx/id1124348115?mt=8';
} else if ( /android/.test(ua)) { // 安卓系统 -> 跳安卓端下载地址
//window.location.href = 'http://xxxxxxx.cn/release/xxxx-release.apk';
alert("android");
} else if (IsPC()){ // PC端
alert("pc");
}
}
</script>
</body>
</html>
方法2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
goDownload();
function goDownload() {
var u = navigator.userAgent, app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
// 是安卓浏览器
if (isAndroid) {
// window.location.href = 'http://xxxxxxx.cn/release/xxxx-release.apk'; // 跳安卓端下载地址
alert("android");
}
// 是iOS浏览器
if (isIOS) {
// window.location.href = 'https://itunes.apple.com/cn/app/xxxxxxx/id1124348115?mt=8'; // 跳AppStore下载地址
alert("ios");
}
// 是微信内部webView
if (is_weixn()) {
alert("wx");
}
// 是PC端
if (IsPC()) {
// window.location.href = 'http://www.xxxxxxx.cn/index.html'; // 公司主页
alert("pc");
}
}
// 是微信浏览器
function is_weixn(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
return true;
} else {
return false;
}
}
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
</script>
</body>
</html>
参考链接:https://www.jianshu.com/p/f9db096857c2