如何根据移动设备的使用情况替换超链接

时间:2012-09-19 11:54:12

标签: html mobile blackberry user-agent

我有以下html检测useragent是否来自黑莓设备。我想知道如何用一个特定的设备替换下载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> 

2 个答案:

答案 0 :(得分:0)

我希望这有助于我完全理解你的要求。一切顺利。

<!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> 

答案 1 :(得分:0)

如果您只想为下载按钮设置特定样式,那么只需添加一个类(如果它是Blackberry),如果不是,则将其删除。