Random rotating banner assistance

Hello all. I found some JavaScript for displaying a rotating banner on a webpage. It works perfectly with one exception, it goes in sequential order of the sequence I have the images set up in the JavaScript. I was hoping to find something that would randomize it. If I have 20 or so logos to scroll through, people at the bottom of the list won’t be seen. My code is below:

<script language="JavaScript1.2">

var howOften = 5; //number often in seconds to rotate
var current = 0; //start the counter at 0
var ns6 = document.getElementById&&!document.all; //detect netscape 6

// place your images, text, etc in the array elements here
var items = new Array();
    items[0]="<a href='sponsor.php' ><img alt='Sponsorship' src=' http://www.hyhpawardshow.com/images/1.png' height='200' width='200' border='0' /></a>"; //a linked image
    items[1]="<a href='https://scarehollow.wixsite.com/haunt' ><img alt='Scare Hollow' src=' http://www.hyhpawardshow.com/images/2.jpg' height='200' width='200' border='0' /></a>";
	items[2]="<a href='http://www.samhaynes.moonfruit.com/' ><img alt='Sam Haynes' src=' http://www.hyhpawardshow.com/images/3.jpg' height='200' width='200' border='0' /></a>";
	
function rotater() {
    document.getElementById("placeholder").innerHTML = items[current];
    current = (current==items.length-1) ? 0 : current + 1;
    setTimeout("rotater()",howOften*1000);
}

function rotater() {
    if(document.layers) {
        document.placeholderlayer.document.write(items[current]);
        document.placeholderlayer.document.close();
    }
    if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
        if(document.all)
            placeholderdiv.innerHTML=items[current];

    current = (current==items.length-1) ? 0 : current + 1; //increment or reset
    setTimeout("rotater()",howOften*1000);
}
window.onload=rotater;
//-->
</script>

I realize this code may be a bit out of date, but I’m hoping to alter it is a simple task rather than having to find different code and try to make it work. My assumption is this just needs to be changed to random:

    `current = (current==items.length-1) ? 0 : current + 1;` 

Can anyone assist on how to fix this script or point me in a new direction?

Thanks

Depends on HOW you want things randomized?

Is this RANDOMLY set once (each) time the page loads?

Or is this random within the same ‘viewing’ (page load?)

You could just shuffle the array order around… and on each page load… the banner order will be different.

If you want it to be dynamically random within the same page load/viewing… then you’ll have to actively change/update it after each banner load.

Might get a bit more involved if you want to try and rule out duplicates in the same ‘viewing/page load’…

maybe something like:

shuffle(items);


Non-project related example:
var myArray = ['1','2','3','4','5','6','7','8','9'];
shuffle(myArray);

throw in the shuffle(myArray); line where you see fit… after the intitial array population or something…

I really don’t care about duplicate showings, I just want it to be random whenever someone visits the page.

Hope that helps? LOL

Did my code/suggestion not help anything? Did you try it?

You should be able to just use this:

shuffle(items);
items

is your banner array…correct?

So if you shuffle it before you plan on loading any images… the order should be randomized.

Hey Whisper, sorry, the email I got for your response fell into my spam folder for some reason. Yes, I did try it and it worked, thank you very much for your input!

Sponsor our Newsletter | Privacy Policy | Terms of Service