Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random background image

Status
Not open for further replies.

joleencronin

IS-IT--Management
Feb 21, 2007
1
NL
Hi,

I want to have a random background image on my page.

I have created the following function random() which calls a random image from a folder.

I then want to load this function when my pages loads, which i have done in the body tag.

the code i have written is posted below. please can you help to make it work??????

thanks



<html>
<head>

<script language=JavaScript> <!--

function random(){

document.body.style.background("<SRC='background/" + Math.ceil(Math.random() * 3) + ".jpg' WIDTH=1024 HEIGHT=634 BORDER=0 >")

//-->

}
</script>


<title>random background</title>

<body onLoad="random()">

</body>
</html>
 
>document.body.style.background("<SRC='background/" + Math.ceil(Math.random() * 3) + ".jpg' WIDTH=1024 HEIGHT=634 BORDER=0 >")

[tt]document.body.style.backgroundImage="url(background/" + Math.ceil(Math.random() * 3) + ".jpg)";[/tt]

ps:Based on the original path to the jpg - no idea whether it is right or wrong.
 
function random(){

document.body.style.background("<SRC='background/" + Math.ceil(Math.random() * 3) + ".jpg' WIDTH=1024 HEIGHT=634 BORDER=0 >")

//-->

}


I think the code is a bit crowded. It's ok to break it into a few lines so you understand what you were doing and why.

function randomPicture()
{
// Get a random number, based on how many pictures you have.
var pictureNumber=Math.floor(Math.random()*3);

/* All your pictures should have same name, except for
number at the end, such as picture1.jpg, picture2.jpg, etc. Otherwise, you could go with an array for pics with different names. */
var url="background/picture"+pictureNumber+".jpg";

document.body.style.backgroundImage="url("+url+")";
}// end randomPicture

Code not tested for syntax errors...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top