Forum Moderators: open
i.e. randomly get <table background="image1.jpg"> OR <table background="image2.jpg"> when the page loads.
I tried doing this several different ways, including:
imgswap = new Array('<table border="0" cellpadding="0" cellspacing="0" width="700" height="256" background="DNAmanBLANK.jpg">','<table border="0" cellpadding="0" cellspacing="0" width="700" height="256" background="DNAwomanBLANK.jpg">');
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
and then in the body of the page:
<script language="JavaScript">
document.write(imgswap[rand(imgswap.length)-1])
</script>
<tr >
<td height="8" width="231"></td>
....... //rest of code
</table>
BUT this only results in no table being displayed. (I know JS works on the page as the other functions work fine). Is there a way to do what I want?
PLEASE HELP! I'm new to JavaScript and trying to learn but I can't find anything but onMouseOver-like functions and that's NOT what I want.
Thanks in advance!
You've got your entire table relying on javascript being turned on to even show up.
If you do decide to go with javascript, hard code the table and use image replace style javascript (similar to a mouseover). Your randomimzer could be simplified as well:
rNum = Math.floor(Math.random() * x)
Where X is the number of whole number options you want.
Added: To clarify... By (similar to a mouseover) I mean use the DOM to display the background image instead of writing in the actual HTML with javascript.
But either way you should be using server side scripting :-)
my solution looks like this:
function pigeBillede ( navn , farve , tekst , xPlacering , yPlacering )
{
this.navn = navn ;
this.farve = farve ;
this.tekst = tekst ;
this.xPlacering = xPlacering ;
this.yPlacering = yPlacering ;
}
var baggrundsBilleder = new Array () ;
baggrundsBilleder[baggrundsBilleder.length] = new pigeBillede ("Alihahd-orakel.jpg","#000000","#0000ff","top","center") ;
baggrundsBilleder[baggrundsBilleder.length] = new pigeBillede ("Alihahd-Sam.jpg","#000000","#ffffff","bottom","center") ;
baggrundsBilleder[baggrundsBilleder.length] = new pigeBillede ("Alihahd-Sam2.jpg","#000000","#ffffff","bottom","center") ;
. . . etc for as many pictures as you want
var baggrund = Math.floor(Math.random()*baggrundsBilleder.length) ;
var test = document.getElementById('siden').style.color = baggrundsBilleder[baggrund].tekst ;
test = document.getElementById('siden').style.background = " no-repeat url(/Pigerne/baggrunde/"+baggrundsBilleder[baggrund].navn+") "+baggrundsBilleder[baggrund].farve+" "+baggrundsBilleder[baggrund].xPlacering+" "+baggrundsBilleder[baggrund].yPlacering ;
your milage may vary, and as you can see I also manipulate the colours of the page and text as well as I can set the position of the image.
I hope you can get my drift despite the danish variable names