Banner rotator at specified %

o hai guyz

New member
Jan 15, 2010
917
8
0
I wanted a banner rotator script so I could display a few different ads/links, and I found this - Javascript: Banner Rotator

Looks pretty good but I'm curious if there's a way to specify the chance that each image shows up. For example if I have 3 banners, I might want #1 to show 80% of the time, #2 to show 15% of the time, and #3 to show 5% of the time. This script looks like it picks one at random so they each have an equal chance of appearing.
 


Try this instead:
Code:
<script language="Javascript">
<!--
// original content taken from Nic's JavaScript Page with permission
// lack of these three lines will result in copyright infringment
// made by: Nic's JavaScript Page - http://www.javascript-page.com

var core = 0;

function initArray() {

this.length = initArray.arguments.length;
  for (var i = 0; i < this.length; i++) {
  this[i] = initArray.arguments[i];
  }
}

link = new initArray(
"http://3bgraphics.hypermart.net/",
"http://html.digitalsea.net/",
"http://www.javascript-page.com/"
);

image = new initArray(
"http://3bgraphics.hypermart.net/link/3bgraphics.gif",
"http://html.digitalsea.net/htmlnow.gif",
"http://www.geocities.com/~jsmaster/jsnow.gif"
);

text = new initArray(
"3B Graphics",
"Learn HTML in 7 Easy Steps",
"Nic's JavaScript Page"
);

var percentage = Math.floor(Math.random()*100);
// Note the variable percentage is a random number from 0 to 99

     if (percentage < 20) core = 0;
else if (percentage < 50) core = 1;
else if (percentage < 100) core = 2;
else document.write('Error in percentage.');

var ranlink  = link[core];
var ranimage = image[core];
var rantext  = text[core];

document.write('<a href=\"' +ranlink+ '\"><img src=\"'+ranimage+'\" border="0" alt=\"'+rantext+'\"></a>');

//-->
</SCRIPT>
To change the percentages, you'll mess with this piece:

Code:
     if (percentage < 20) core = 0;
else if (percentage < 50) core = 1;
else if (percentage < 100) core = 2;
else document.write('Error in percentage.');
The code I gave above has a 20% chance for core = 0, 30% chance for core = 1, and 50% chance for core = 2.

Hope this helps.
 
  • Like
Reactions: o hai guyz
Strange issue that happens even when using the original script without the percentages - I put it in the body of a blank html page with nothing else on it. Works perfectly when I run it from my computer, but when I upload it certain images never show up (I did upload them all correctly) and instead show the alt text. It's always the same images that never work, although when I open the file on my hard drive it works perfectly.

If you want, PM me your Skype/AIM username and I'll send you the page. Just don't want to post the entire thing here.
 
Strange issue that happens even when using the original script without the percentages - I put it in the body of a blank html page with nothing else on it. Works perfectly when I run it from my computer, but when I upload it certain images never show up (I did upload them all correctly) and instead show the alt text. It's always the same images that never work, although when I open the file on my hard drive it works perfectly.

If you want, PM me your Skype/AIM username and I'll send you the page. Just don't want to post the entire thing here.

Edit: Figured it out. Apparently capitalization doesn't matter when it's run from your hard drive but it does matter when it's run from the server. I didn't know that. Some of my ads were labeled "300X250-adname.jpg" and then in the script I linked to them as "300x250-adname.jpg" (lowercase X).
 
Hi potentialeight, This is Ruth Willard thanking you for your JavaScript programming for banner rotators.