【发布时间】:2021-10-25 12:35:53
【问题描述】:
我只是创建一个页面来将用户重定向到应用程序链接(google play & apple store & Huawei store)
我只是从this链接获取代码
代码是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Test</title>
<script>
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (/android/i.test(userAgent)) {
return "Android";
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
</script>
</head>
<body onload="DetectAndServe()">
<script>
function DetectAndServe() {
let os = getMobileOperatingSystem();
if (os == "Android") {
window.location.href = "https://play.google.com";
} else if (os == "iOS") {
window.location.href = "https://apps.apple.com";
} else {
window.location.href = "https://my-link.com";
}
}
</script>
</body>
</html>
我们知道华为有自己的商店,我如何检查设备是否是华为重定向到华为链接?
【问题讨论】:
-
@Justinas 这就像我的代码有问题! “我需要检查
Huawei device! -
@phuzi 这就像我的代码有问题! “我需要检查
Huawei device! -
您在检查 Android 之前是否尝试过
if (/huawei/i.test(userAgent)),因为它可以通过两项测试?
标签: javascript html