PDA

View Full Version : adding random BG variable


clevert
08-11-2002, 01:35 AM
<script>

Hi, I'm trying to add more backgrounds to this random background generator. I tried several combinations of repeating commands but nothing. I would be so very grateful for any leads.
here's the code

/*Random Background Image-
By JavaScript Kit (www.javascriptkit.com)
More free JavaScripts here!
*/

var backgr1="BGbark.jpg"
var backgr2="BGcloud.jpg"
var backgr3="BGforestspirit.jpg"

var cur=Math.round(4*Math.random())
if (cur<=1)
backgr=backgr1
else if (cur<=4)
backgr=backgr2
else
backgr=backgr3

document.write('<body background="'+backgr+'" bgcolor="#FFFFFF">')
</script>

joh6nn
08-11-2002, 04:37 AM
here's a function that will give you a random number. if you pass it one number, it will give you a random number between 0, and that number. if you pass it two numbers, it will return a random number between those two numbers.

function rand_int(num_1, num_2)
{
return ( (num_2) ? Math.floor(Math.random() * (num_2 - num_1) ) + num_1 : Math.floor(Math.random() * num_1));
}

if you make an array, then you can refer to the things in that array by number. that means, if you put the random number you get from the above function, into the array, then you get a random item from in that array.

</script>
function rand_int(num_1, num_2)
{
return ( (num_2) ? Math.floor(Math.random() * (num_2 - num_1) ) + num_1 : Math.floor(Math.random() * num_1));
}
var bgs = ["BGbark.jpg", "BGcloud.jpg", "BGforestspirit.jpg"];
var rand = rand_int(bgs.length);
document.write('<body background="' + bgs[rand] + '" bgcolor="#FFFFFF">');
</script>