【问题标题】:HTML Script redirection to website, or app storesHTML 脚本重定向到网站或应用商店
【发布时间】:2016-02-18 13:17:03
【问题描述】:

我想制作一个 HTML,并根据您访问网络的设备转到 iTunes 或 Google Play。

例如:

midominio.com/hola.html

如果我从网络访问,请访问 midominio.com 如果我从 iphone 访问,ipad 转到 iTunes 谷歌地图 (APP) 如果我从 Android 访问,请转到 Google Play 谷歌地图(APP)

谢谢

【问题讨论】:

标签: javascript html redirect


【解决方案1】:

首先,检测你是在什么类型的设备上

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

然后玩弄你的链接可见性

if(isAndroid) {
    document.getElementById('android').style.display = 'initial';
  document.getElementById('ios').style.display = 'none';
}

if(isIOS) {
    document.getElementById('android').style.display = 'none';
  document.getElementById('ios').style.display = 'initial';
}

if(!isAndroid && !isIOS) {
    document.getElementById('android').style.display = 'initial';
  document.getElementById('ios').style.display = 'initial';
}

看看这个jsFiddle

【讨论】:

  • 如果用户代理同时匹配androidiPad|iPhone|iPod怎么办?
  • 基本不行,但是你可以把!isAndroid && !isIOS改成!isAndroid && !isIOS || isAndroid && isIOS
【解决方案2】:

您好可以通过navigator.userAgent获取详细信息 检查下面的代码。

<div id="demo"></div>

<script>
var txt = "";
txt += "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt += "<p>Browser Name: " + navigator.appName + "</p>";
txt += "<p>Browser Version: " + navigator.appVersion + "</p>";
txt += "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt += "<p>Browser Language: " + navigator.language + "</p>";
txt += "<p>Browser Online: " + navigator.onLine + "</p>";
txt += "<p>Platform: " + navigator.platform + "</p>";
txt += "<p>User-agent header: " + navigator.userAgent + "</p>";

document.getElementById("demo").innerHTML = txt;
</script>

我的浏览器的输出是

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

Cookies Enabled: true

Browser Language: en-US

Browser Online: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

希望对你有所帮助...

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2014-04-06
    • 1970-01-01
    • 2022-01-17
    • 2012-11-11
    相关资源
    最近更新 更多