Forum Moderators: open

Message Too Old, No Replies

Random Table Background

With Includes

         

norml

3:53 am on May 24, 2003 (gmt 0)

10+ Year Member



I need to implement a random table background on pageload.
This thread [webmasterworld.com] has the javascript code for it, and it works great, but I need to include an external file.
So far nothing has worked.

Here's what I'd like to do:


<script language="JavaScript">
imgswap = new Array('<td background="image.jpg">[b]<?php include "file.txt";?>[/b]</td>','<td background="image2.jpg">[b]<?php include "file.txt";?>[/b]</td>');

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);
};
</script>


<table>
<tr>
<script language="JavaScript">
document.write(imgswap[rand(imgswap.length)-1])
</script>
</tr>
</table>

Or any kind of include that would work. Any thoughts on this?

rainborick

2:35 pm on May 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't embed php directives into a document using JavaScript because the JavaScript doesn't get interpreted and executed until it reaches the user's browser - way too late for the server to intercept and execute the embedded php. You need something more along the lines of:

<script language="JavaScript">
imgswap = new Array('<td background="image.jpg">','<td background="image2.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);
};
</script>


And then embed in your HTML, something like:

<script type="text/javascript" language="JavaScript"><!-- //
doucment.write(imgswap[rand{0)]);
// --></script>
<?php include "file.txt";?></td>

norml

3:01 pm on May 24, 2003 (gmt 0)

10+ Year Member



That worked.

Thanks for the help!