SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pass link to anchor from Javascript

    Hi all

    I'm newish to Javascript but I come from using Actionscript for years.

    I have a simple a tag in html that when clicked I want to link to a random address picked from an array of possible links.

    So in the code here I have an array of the links and I'm picking a random number from the length of that array, I'm then using that number to pick a random link from the array.

    Code:
    <script type="text/javascript">
    		  var links_arr = ["http://www.link1.com","http://www.link2.com","http://www.link3.com","http://www.link4.com"]
          function link(){
            var ranNum = Math.floor(Math.random()*links_arr.length)
            var linkTo = links_arr[ranNum];
            window.alert(linkTo);
          }
    This all works fine and the window.alert show randomly picked links from the array.

    The problem is passing the link to the href of the a tag - how can I use the link in the href ?

    Code:
    <a class="buy" href="javascript:link()" target="_blank">buy</a>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    window.location maybe?

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ttmt View Post
    The problem is passing the link to the href of the a tag - how can I use the link in the href ?

    Code:
    <a class="buy" href="javascript:link()" target="_blank">buy</a>
    Do you want the link to get the random link before the person clicks on it, or do you want the random link to be chosen when the person clicks on the link?

    Tip: the second option is a lot easier to do.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the person clicks the link.

    I can pick the link randomly, it was just passing it to the a tag

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ttmt View Post
    When the person clicks the link.

    I can pick the link randomly, it was just passing it to the a tag
    In that case you just want to set window.location to the new link.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •