#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2015
    Location
    Caracas
    Posts
    2
    Rep Power
    0

    Question how to change anchor href every 5 seconds


    hello i have a website where i want to show some amazon associates ads, but i want that the ads will change every 5 seconds,
    i found this piece of javascript code that will change images every 5 seconds i have modified, but i don't know how to put the
    <a /a> variable links into javascript to let it show the banner from amazon

    Code:
    <script type="text/javascript">
        $(function(){
            //prepare Your data array with img urls
            var dataArray=new Array();
            dataArray[0]="<a link and image from amazon />";
            dataArray[1]="<a link and image from amazon />";
            dataArray[2]="<a link and image from amazon />";
            dataArray[3]="<a link and image from amazon />";
    
            //start with id=0 after 5 seconds
            var thisId=0;
    
            window.setInterval(function(){
                (here must go the code to show the amazon ads banner)
                thisId++; //increment data array id
                if (thisId==3) thisId=0; //repeat from start
            },5000);        
        });
    </script>
    anyone can help me to figure out how to do this ? i have spent almost all the afternoon searching on google but i haven't found
    anything that work for me.

    thanks in advance for all the help.
  2. #2
  3. ~ bald headed old fart ~
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    chertsey, a small town s.w. of london, england
    Posts
    363
    Rep Power
    133
    Hi there
    daimaku,

    and a warm welcome to these fourms.

    Try it like this...

    Code:
    
    <!DOCTYPE html>
    <html  lang="en">
    <head>
    
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>untitled document</title>
    
    </head>
    <body>
    
    <a id="myhref" href="http://www.example1.com">example1.com</a>
    
    <script>
    (function() {
       'use strict';
      
       var urls=['example1.com','example2.com',
              'example3.com','example4.com'
             ];
       var c=0,link=document.getElementById('myhref'),delay=5000;
    
    setInterval(
       function(){
       c++;
    if(c===urls.length){
       c=0;
     }
       link.href='http://www.'+urls[c];
       link.innerHTML=urls[c];
     },
       delay
    );
    
    })();
    </script>
    
    </body>
    </html>
    coothead

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping out here! :)
    ~ the original bald headed old fart ~
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2015
    Location
    Caracas
    Posts
    2
    Rep Power
    0
    thank you coothead, it worked just fine, that's what i was looking to do, thank you very much !!! you are awesome !
  6. #4
  7. ~ bald headed old fart ~
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    chertsey, a small town s.w. of london, england
    Posts
    363
    Rep Power
    133

    No problem, you're very welcome.


    coothead
    ~ the original bald headed old fart ~

IMN logo majestic logo threadwatch logo seochat tools logo