如何基于移动设备的用户代理替换超链接
Posted
技术标签:
【中文标题】如何基于移动设备的用户代理替换超链接【英文标题】:How to replace hyperlink based on useragent of mobile device 【发布时间】:2012-09-11 17:33:12 【问题描述】:我有以下 html 来检测用户代理是否来自黑莓设备。我想知道如何用特定设备的下载 url 替换下载 url,即,如果他的设备是 9800,我想指导用户下载 9800 设备。请有人帮忙吗?
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var ua = navigator.userAgent;
document.write("BB OS Version :: " + ua);
if (ua.indexOf("BlackBerry") >= 0)
if (ua.indexOf("Version/") >= 0) // ***User Agent in BlackBerry 6 and BlackBerry 7
Verposition = ua.indexOf("Version/") + 8;
TotLenght = ua.length;
document.write("BB OS Version :: " + ua.substring(Verposition, Verposition + 3));
else // ***User Agent in BlackBerry Device Software 4.2 to 5.0
var SplitUA = ua.split("/");
document.write("BB OS Version :: " + SplitUA[1].substring(0, 3));
</script>
<br>
<a href="http://mysite.com/download">Download</a>
</body>
</html>
【问题讨论】:
查看此链接,***.com/questions/4365246/…。 【参考方案1】:我希望这会有所帮助,并且我完全理解您的要求。一切顺利。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a href="#" id="theLink">Download</a><br>
<script type="text/javascript">
function set_url(id, url)
document.getElementById(id).href = url;
var ua = navigator.userAgent;
document.write("BB OS Version :: " + ua);
if (ua.indexOf("BlackBerry") >= 0)
if (ua.indexOf("Version/") >= 0) // ***User Agent in BlackBerry 6 and BlackBerry 7
Verposition = ua.indexOf("Version/") + 8;
TotLenght = ua.length;
document.write("BB OS Version :: " + ua.substring(Verposition, Verposition + 3));
set_url("theLink", "http://www.google.com"); // go to User Agent in BlackBerry 6 and BlackBerry 7 url
else // ***User Agent in BlackBerry Device Software 4.2 to 5.0
var SplitUA = ua.split("/");
document.write("BB OS Version :: " + SplitUA[1].substring(0, 3));
set_url("theLink", "http://www.yahoo.com"); // go to User Agent in BlackBerry Device Software 4.2 to 5.0 url
</script>
</body>
</html>
【讨论】:
【参考方案2】:如果您只想为下载按钮设置特定样式,那么如果是 Blackberry,则只需添加一个类,如果不是,则将其删除。
【讨论】:
以上是关于如何基于移动设备的用户代理替换超链接的主要内容,如果未能解决你的问题,请参考以下文章