Click to See Complete Forum and Search --> : Random Background


lancehyde
01-16-2004, 02:47 PM
I am using a script that randomizes the background pictures.
The problem I am having is that the picture doesn't appear fully. Here is my code.

You can visit the page at

http://lancehyde.homeftp.net:8080/test.html

<html>
<head>
<title>Lance Hyde's Web Site!</title>

<SCRIPT LANGUAGE="JavaScript">

today=new Date();
jran=today.getTime();
var number = 10;
var random_number="";
var image="";
var text_color="";
ia=9301;
ic=49297;
im=233280;
jran = (jran*ia+ic) % im;
random_number = Math.ceil( (jran/(im*1.0)) *number);
// Loads the appropriate image and text color based on random number.
if (random_number==1) {
text_color="000000";
image="1.jpg";
}
if (random_number==2) {
text_color="000000";
image="2.jpg";
}
if (random_number==3) {
text_color="000000";
image="3.jpg";
}
if (random_number==4) {
text_color="000000";
image="4.jpg";
}
if (random_number==5) {
text_color="000000";
image="5.jpg";
}
if (random_number==6) {
text_color="000000";
image="6.jpg";
}
if (random_number==7) {
text_color="000000";
image="7.jpg";
}
if (random_number==8) {
text_color="000000";
image="8.jpg";
}
if (random_number==9) {
text_color="000000";
image="9.jpg";
}
if (random_number==10) {
text_color="000000";
image="10.jpg";
}
// End -->
</SCRIPT>
</head>

<body>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
document.open();
document.write("<BODY BACKGROUND='"+image+"' TEXT='"+text_color+"'>");
document.write("<CENTER></CENTER>");
// End -->
</SCRIPT>


</body>
</html>

fredmv
01-16-2004, 04:06 PM
I tried looking at the link you've provided but it never loaded for me. However, that script you currently have is very poorly coded. I've just written this — simply customize it to your needs (changing the image filenames and text colors) and it should work just fine:<script type="text/javascript">
//<![CDATA[
var data =
[
'1.png|#900',
'2.png|#090',
'3.png|#009'
];

var info = data[Math.floor(Math.random()*data.length)].split('|');


onload = function()
{
with(document.body.style)
{
backgroundImage = 'url(' + info[0] + ')';
color = info[1];
}
}
//]]>
</script>

lancehyde
01-21-2004, 07:25 PM
I have 10 pictures, how do I add more than 3?

fredmv
01-21-2004, 07:56 PM
In the exact same format I have above. For example, if you wanted to add another:var data =
[
'1.png|#900',
'2.png|#090',
'3.png|#009',
'4.png|#099'
];And so on. You can also changes the values after the "|" to whatever color you want.

Kor
01-22-2004, 03:08 AM
To fredmv

Interesting, never used/saw

with() function

What is that for? Which is its browser types limmitation?

Pittimann
01-22-2004, 03:19 AM
Hi!

To Kor:

you can find a description of the with - statement here:

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004910

As far as I know, there are no browser limitations for the statement itself. They might occur depending on the code inside the

with(blah){
code
}

Cheers - Pit

fredmv
01-22-2004, 10:07 AM
It's basically used to save typing. In my previous piece of code, the with statement allows it so that document.body.style is already written before the lines inside of it. As for browser support, it was introduced in JavaScript 1.3 (i.e., Netscape 4.x). So, generally any browser that supports JavaScript should support it.

Kor
01-23-2004, 02:02 AM
Thanks all for explanations.