2012-09-19 48 views
4

我有以下html檢測useragent是否來自黑莓設備。我想知道如何用一個特定的設備來替換下載url,也就是說,如果他的設備是9800,我想引導用戶下載9800設備。請任何人都可以幫忙嗎?如何替換基於移動設備的useragent的超鏈接

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

檢查此鏈接,http://stackoverflow.com/questions/4365246/how-to-change-href-of-a-tag-on-button-click-through-javascript/4365272#4365272。 – Rupak

回答

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

如果你只是想爲你的下載按鈕的具體樣式,則只需添加一個類,如果它是一個黑莓手機,並把它拿走,如果事實並非如此。