Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Make a rotating banner box random

  • 11-04-2012 11:00AM
    #1
    Registered Users, Registered Users 2 Posts: 2,200 ✭✭✭


    Hey,

    I have a rotating banner box on my site, but it is in a set pattern, ie, it always goes 1,2,3,4,5,6. Is there a way to randomize that, so that you don't know which order will be shown when a visitor enters the site?

    The code I'm using is
    <HTML><HEAD>
    <TITLE>Rotating banners with links</TITLE>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Beginning of JavaScript -
    
    if (document.images) {
    ads = new Array(5);
    ads[0] = "http://img97.imageshack.us/img97/3014/tophk.jpg";
    ads[1] = "http://www.seairsoft.net/images/seairsoft_header.gif";
    ads[2] = "http://img180.imageshack.us/img180/2585/miavinyl2.jpg";
    ads[3] = "http://www.mwairsoft.net/img/logo.jpg";
    ads[4] = "http://www.reload.ie//wp-content/themes/simploblack/images/logo.png";
    ads[5] = "http://img97.imageshack.us/img97/3014/tophk.jpg";
    
    }
    
    newplace = new Array(5);
    newplace[0] = "http://www.hobbyairsofter.com/"
    newplace[1] = "http://www.seairsoft.net/"
    newplace[2] = "http://www.mainirishairsoft.com/scripts/default.asp"
    newplace[3] = "http://www.mwairsoft.net"
    newplace[4] = "http://www.reload.ie/"
    newplace[5] = "http://www.hobbyairsofter.com/"
    
    var timer = null
    var	 counter = 0
    
    function banner() {
    	    timer=setTimeout("banner()", 5000);
    		counter++;
    		if (counter >= 5)
    		counter = 0;
    		document.bannerad.src = ads[counter];
    }
    
    function gothere() {
    		counter2 = counter;
    		window.location.href = newplace[counter2];
    }
    
    // - End of JavaScript - -->
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" onload="banner()">
    
    <a href="javascript:gothere()"><IMG SRC="http://www.seairsoft.net/images/seairsoft_header.gif" WIDTH="350" HEIGHT="100" BORDER="0" NAME="bannerad"></a>
    
    </BODY>
    </HTML>
    

    Is there a way to alter this script or will I need to scrap it and start again?


Comments

  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    instead of your counter variable.


    var total = ads.length;
    var randomNum = Math.floor((Math.random()*total)+1);
    document.bannerad.src = ads[randomNum];

    * where total is the total amount of elements in your array.


  • Registered Users, Registered Users 2 Posts: 2,200 ✭✭✭J.D.R


    instead of your counter variable.


    var total = ads.length;
    var randomNum = Math.floor((Math.random()*total)+1);
    document.bannerad.src = ads[randomNum];

    * where total is the total amount of elements in your array.

    So, my code would look like this??
    var timer = null
    var total = ads.length;
    var randomNum = Math.floor((Math.random(5)*total)+1);
    document.bannerad.src = ads[randomNum];
    


Advertisement