Script Code: How to rotate through random banners

Discussion in 'Scripts' started by Mrs. Sinner, Jan 4, 2009.

  1. #1
    Here are a couple of codes to have random banners rotate through on your web page for those who need them. Replace the parts in red with the banner info you are using.

    This one is for banners that will rotate every few seconds without refreshing or reloading the page:
    <script type="text/javascript">
    var imgs1 = new Array("[COLOR="Red"]myimage1[/COLOR]","[COLOR="Red"]myimage2[/COLOR]");
    var lnks1 = new Array("[COLOR="Red"]mylink1[/COLOR]","[COLOR="Red"]mylink2[/COLOR]");
    var alt1 = new Array("[COLOR="Red"]myalt1[/COLOR]","[COLOR="Red"]myalt2[/COLOR]");
    var currentAd1 = 0;
    var imgCt1 = 2;
    function cycle1() {
      if (currentAd1 == imgCt1) {
        currentAd1 = 0;
      }
    var banner1 = document.getElementById('adBanner1');
    var link1 = document.getElementById('adLink1');
      banner1.src=imgs1[currentAd1]
      banner1.alt=alt1[currentAd1]
      document.getElementById('adLink1').href=lnks1[currentAd1]
      currentAd1++;
    }
      window.setInterval("cycle1()",10000);
    </script>
    <a href=""[COLOR="Red"]mylink1[/COLOR]"" id="adLink1" target="_top">
    <img src="[COLOR="Red"]myimage1[/COLOR]" id="adBanner1" border="0" width="728" height="90"></a>
    Code (markup):
    Here is a code that will rotate through random banners each time the page is visited or reloaded:
    <SCRIPT LANGUAGE="JavaScript">
    
    
    
    
    
    <!-- Begin
    
    
    
    var maxAdNo = [B][COLOR="Blue"]1[/COLOR][/B]
    
    var adNo
    
    var myAd = new Array()
    
    
    
    myAd[0] = '[COLOR="Red"]<a href="http://website1.com"><img style="border: none;" src="http://website1simagesource.com" alt="website1"/></a>[/COLOR]'
    
    myAd[1] = '[COLOR="Red"]<a href="http://website2.com"><img style="border: none;" src="http://website2simagesource.com" alt="website2"/></a>[/COLOR]'
    
    
    //  End -->
    
    </script>
    
    
    
    
    
    
    <SCRIPT LANGUAGE="JavaScript">
    
    
    
    <!-- Begin
    
      adNo = Math.round(Math.random() * maxAdNo)
    
      document.write(myAd[adNo])
    
    //  End -->
    
    </script>
    
    
    
    Code (markup):
    You can add as many additional banners as you need, just be sure to change the blue number to include them all in your random rotation.

    It's working on my site, but if there's anything wrong with the codes or any room for improvement, please feel free to share. I'm still learning myself, mainly by googling and burying my nose in "... For Dummies" books.
     
    Mrs. Sinner, Jan 4, 2009 IP
  2. ruberr2002

    ruberr2002 Peon

    Messages:
    463
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thanks Mrs Sinner for posting this useful script. I will give it a try.
     
    ruberr2002, Jan 4, 2009 IP