4

我有以下 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> 
4

2 回答 2

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> 
于 2012-09-22T22:53:50.267 回答
0

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

于 2013-01-21T12:15:26.553 回答