The script (save as rand_bg.js):
Code:
/***********************************************
* Random Element Backgrounds - adapted from:
* Random Content Colors script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
//specify list of random background images and colors to apply to CSS class "randomimage"
//in the form - "BackgroundImage:BackgroundColor" (required)
//For each entry, you can also specify a text and link color via the syntax:
// "BackgroundImage:BackgroundColor:TextColor" (recommended)
// OR "BackgroundImage:BackgroundColor:TextColor:LinkColor" (optional)
var randombgs=["photo1.jpg:green:white:yellow", "photo2.jpg:#DDF4FF", "photo3.jpg:#FFFF97", "photo4.jpg:#CFFF9F"]
var rbcssrule=""
var randomnum=Math.floor(Math.random()*randombgs.length)
if (randombgs[randomnum].indexOf(":")!=-1&&randombgs[randomnum].split(":").length>2){
rbcssrule+="background-image: url("+randombgs[randomnum].split(":")[0]+");"
rbcssrule+="background-color: "+randombgs[randomnum].split(":")[1]+";"
rbcssrule+="color: "+randombgs[randomnum].split(":")[2]+";"
}
else{
rbcssrule+="background-image: url("+randombgs[randomnum].split(":")[0]+");"
rbcssrule+="background-color: "+randombgs[randomnum].split(":")[1]+";"
}
document.write('<style type="text/css">\n')
document.write('.randomimage{'+rbcssrule+'}\n')
if (randombgs[randomnum].split(":").length==4) //if link color specified
document.write('.randomimage a{color:'+randombgs[randomnum].split(":")[3]+';}\n')
document.write('<\/style>')
Example page:
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="rand_bg.js" type="text/javascript">
/***********************************************
* Random Element Backgrounds - adapted from:
* Random Content Colors script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
</script>
</head>
<body>
<div class="randomimage">Hi<br> <br> </div>
</body>
</html>
Notes: The class name is now 'randomimage' and can be used with td's or whatever. If the script is not in the same folder as the page, the path to it (preferably absolute) must be specified on the page and the paths to the images used in the script must be specified (also preferably absolute).
Example of an absolute path:
Code:
http://www.somedomain.com/files/images/photo1.jpg
Bookmarks