Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Make a rotating banner box random

Options
  • 11-04-2012 12:00pm
    #1
    Registered Users 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 Posts: 26,558 ✭✭✭✭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 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