ant*_*eys 9 javascript jquery android ios windows-phone-8
我想创建一个简单的按钮,将移动用户重定向到相应的应用商店链接,具体取决于他们正在运行的移动操作系统(ios,android或wp8) - 或者如果不在移动设备上,则提供发送包含相应链接的电子邮件...有任何想法吗?
嗯......这实际上很简单.首先,您必须通过用户代理字符串检测用户当前正在使用的设备.然后使用jQuery简单地设置href锚元素的属性.以下代码说明.
var operatingSystem, userAgentString = navigator.userAgent;
var link = $("#store");
if (userAgentString.indexOf("iPhone") > -1 || userAgentString.indexOf("iPod") > -1 || userAgentString.indexOf("iPad") > -1) {
operatingSystem = "iOS";
link.attr("href", "http://store.apple.com/us/browse/app");
} else if (/Android/.test(userAgentString)) {
operatingSystem = "Android";
link.attr("href", "https://play.google.com/store/apps?hl=en");
} else if (/Windows Phone/.test(userAgentString)) {
operatingSystem = "Windows Phone";
link.attr("href", "http://www.windowsphone.com/en-us/store");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4879 次 |
| 最近记录: |