Image changer

B

Barely Audible

Anyone recommend a good javascript image changer - preferably one that
does a random image with the ability to post a certain image on a day?

TTFN
Jim
 
A

ASM

Barely Audible a écrit :
Anyone recommend a good javascript image changer - preferably one that
does a random image with the ability to post a certain image on a day?

or it is a random image
or it is the image of the day

or do you mean : random image except on sunday ?
 
B

Barely Audible

or it is a random image or it is the image of the day

or do you mean : random image except on sunday ?

No what I meant was a random image on a normal day but on say xmas day,
easter sunday etc or any other day I nominate a certain image would be
shown.

TTFN
 
A

ASM

Barely Audible a écrit :
No what I meant was a random image on a normal day but on say xmas day,
easter sunday etc or any other day I nominate a certain image would be
shown.

So you'l have
-1- an array for images to randomize
-2- an array with your special images
-3- a function to chose image relatively to the date



var randomImages = new Array();
var specialImages = new Array();

randomImages[0] = '0.jpg';
randomImages[1] = '1.jpg';
....
randomImages[12] = '12.jpg';

specialImages[0] = 'xmas.jpg'
specialImages[1] = 'grd_pa_birthday.jpg'
....
specialImages[56] = 'grd_ma_birthday.jpg'

function choseImage() {
ar pict = '';
var i = Math.random()*randomImages.length;
i = Math.floor(i*10);
var D = new Date();
var d = D.getDate(); if(d<10) d = '0'+d;
var m = D.getMonth()+1*1; if(m<10) m = '0'+m;
D = m+d+''; // D = month + day ( = 1225 for xmas)
var k = false;
switch(i) {
case '1225': k = 0; break;
case '0228': k = 1; break;
...
case '0508': k = 56; break;
}
if(k) pict = specialImages[k];
else pict = randomImages;
return pict;
}

onload = function() { document.images['day_img'].src = choseImage(); }
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
r>, Sun, 25 Feb 2007 20:07:38, ASM <[email protected]
nvalid> posted:
function choseImage() {
ar pict = '';
var i = Math.random()*randomImages.length;
i = Math.floor(i*10);
var D = new Date();
var d = D.getDate(); if(d<10) d = '0'+d;
var m = D.getMonth()+1*1; if(m<10) m = '0'+m;
D = m+d+''; // D = month + day ( = 1225 for xmas)
var k = false;
switch(i) {
case '1225': k = 0; break;
case '0228': k = 1; break;
...
case '0508': k = 56; break;
}
if(k) pict = specialImages[k];
else pict = randomImages;
return pict;
}


No point in choosing your random i until you know it's needed.

One can simplify - consider :

function Random(M) { return Math.floor(M*(Math.random()%1)) }

SI = []
SI[1225] = "Xmas.pic"
SI[229] = "Leap.pic"

RI = ["P0", "P1", "P2"]

D = new Date("2024/02/29")
i = (D.getMonth()+1)*100 + D.getDate()

pict = SI

if (!pict) pict = RI[Random(RI.length)] // FAQ function

pict
 
A

ASM

Dr J R Stockton a écrit :
In comp.lang.javascript message <[email protected]
r>, Sun, 25 Feb 2007 20:07:38, ASM <[email protected]
nvalid> posted: (...)
No point in choosing your random i until you know it's needed.

Hello Doc,

Sure you're right
But ... you know ... and I allready said :
not too much difficult in first approach

Thanks for the shorter view point.
 
A

ASM

David Golightly a écrit :
var randomImages = new Array();
var specialImages = new Array();

randomImages[0] = '0.jpg';
randomImages[1] = '1.jpg';
...
randomImages[12] = '12.jpg';

specialImages[0] = 'xmas.jpg'
specialImages[1] = 'grd_pa_birthday.jpg'
...
specialImages[56] = 'grd_ma_birthday.jpg'

Or (nowadays more common):
var randomImages = ['0.jpg', '1.jpg', '2.jpg', /*...*/ '12.jpg'];

but perhaps less comprehensive ?
Or:

function randomImage(n) { return n+'.jpg'; }

no, because I suppose images could have "normal" names
(as those "special")
I wouln't have had to give them index of array as name :-(
Do you mean "D" ?

it could be :-/
case '1225': k = 0; break;
case '0228': k = 1; break;
...
case '0508': k = 56; break;
}
if(k) pict = specialImages[k];

So what happens if i == '1225'?

Are you mad ? why not 125546789 images ?
Hope OP uses only 3 or 4 images :)
(999 images could be enough no ?)
else pict = randomImages;


Maybe:

// For your normal images:
images = ['1.jpg', '2.jpg' /* ... */ ];


yeap, but it is for lighted users.
// Then, for your special images:
images['1225'] = 'xmas.jpg';
// etc.
// if you have less than 1000 rotating images and your dates always
have 4 digits, just index the array:

if (!(D in images)) D = i;
return images[D];

instead of the switch statement. You keep all your indexing in one
place that way, and don't have arbitrary 'go-between' indices.

Just some thoughts.

not bad, not bad :)

I ask myself why anybody did answer to this question, if so much guys
know better how to do and how to teach it.
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top