Displaying random ads

D

Developer

Ok I have a bunch of ads, so I wanted to show 1 of a bunch. So I figure:

var randomnumber=Math.floor(Math.random()*11); // chenge the muliiplier to the number of ads available

will give me an index. So I have a bunch of ads already coded as document.write statements. So should I use a switch statement or move the ads to an array>

Ideas?
 
S

Stevo

Developer said:
Ok I have a bunch of ads, so I wanted to show 1 of a bunch. So I figure:

var randomnumber=Math.floor(Math.random()*11); // chenge the
muliiplier to the number of ads available

will give me an index. So I have a bunch of ads already coded as
document.write statements. So should I use a switch statement or move
the ads to an array>

Ideas?

I'd use an array holding the URLs. Then you only need one document.write
that takes the URL out of the array based on the random number selected.
You can use the array's .length property for your multiplier. It would
make maintenance a lot easier than having to look in the switch statement.
 
T

Thomas Allen

Ok I have a bunch of ads, so I wanted to show 1 of a bunch. So I figure:

   var randomnumber=Math.floor(Math.random()*11); // chenge the muliiplier to the number of ads available

will give me an index. So I have a bunch of ads already coded as document..write statements. So should I use a switch statement or move the ads to anarray>

Ideas?

I wrote this a while back:

// Takes the target ID, image root directory path, and any number of
image
// filenames, applying them at random as a background
//
// randomBg('targetId','/imgdir', 'img1.png', 'img2.png', 'img3.png');

function randomBg( id, path ) {
var bgimg = new Array();

for ( i = 2; i < arguments.length; i++ ) { // Grab the extra
arguments
bgimg.push( arguments );
};

bgindex = Math.floor(Math.random() * bgimg.length); // Random
number, rounded down
document.getElementById( id ).style.backgroundImage = "url(" +
path + "/" + bgimg[bgindex] + ")"; // Apply random image
}

Thomas
 
T

Thomas Allen

You would just change the last line to element.src, rather than the
background style, for it to work on a normal image element. It doesn't
use document.write because we want an image to show up with JS
disabled (load the placeholder and switch in onload).

Thomas
 
T

Thomas Allen

I would prefer an array instead of passing a variable number of
arguments. Arrays are easier to handle, and you don't have to use the
"arguments" object.

randomBg("targetId", "/imgdir", ["img1.png", "img2.png", "img3.png"]);

...

function randomBg (id, path, list) {

It also makes the function signature clearer.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top