Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Me code

why not run random background image

img1.css
body {
	background-image: url(image1.jpg);
	background-color:#4E0000;
	background-position: left top;
	background-repeat:no-repeat;
}

img2.css
body {
	background-image: url(image2.jpg);
	background-color:#4E0000;
	background-position: left top;
	background-repeat:no-repeat;
}

img3.css
body {
	background-image: url(image3.jpg);
	background-color:#4E0000;
	background-position: left top;
	background-repeat:no-repeat;
}

img4.css
body {
	background-image: url(image4.jpg);
	background-color:#4E0000;
	background-position: left top;
	background-repeat:no-repeat;
}

img5.css
body {
	background-image: url(image5.jpg);
	background-color:#4E0000;
	background-position: left top;
	background-repeat:no-repeat;
}



HTML

<link href="img2.css" rel="stylesheet" type="text/css">
<link href="img1.css" title="img1" rel="alternate stylesheet" type="text/css">
<link href="img3.css" title="img3" rel="alternate stylesheet" type="text/css">
<link href="img4.css" title="img4" rel="alternate stylesheet" type="text/css">
<link href="img5.css" title="img5" rel="alternate stylesheet" type="text/css">
<link href="img6.css" title="img6" rel="alternate stylesheet" type="text/css">
<script type="text/javascript" language="javascript">

<!--

//"manual" or "random"
var manual_or_random="manual"
//"eachtime", "sessiononly", or "x days
var randomsetting="1 days" (replace x with desired integer)". Only applicable if mode is random.



function getCookie(Name)
{
var re=new RegExp(Name+"=[^;]+", "i");
//construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1]
//return its value
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete
//cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function deleteCookie(name){
setCookie(name, "moot")
}


function setStylesheet(title, randomize)
{
//Main stylesheet switcher function. Second parameter if defined causes
//a random alternate stylesheet (including none) to be enabled
var i, cacheobj, altsheets=[""]
for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title"))
{
//if this is an alternate stylesheet with title
cacheobj.disabled = true
altsheets.push(cacheobj)
//store reference to alt stylesheets inside array
if(cacheobj.getAttribute("title") == title)
//enable alternate stylesheet with title that matches parameter
cacheobj.disabled = false
//enable chosen style sheet
}
}
if (typeof randomize!="undefined"){
//if second paramter is defined, randomly enable an alt style sheet (includes non)
var randomnumber=Math.floor(Math.random()*altsheets.length)
altsheets[randomnumber].disabled=false
}
return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : ""
//if in "random" mode, return "title" of randomly enabled alt stylesheet
}

function chooseStyle(styletitle, days){
//Interface function to switch style sheets plus save "title" attr of
//selected stylesheet to cookie
if (document.getElementById){
setStylesheet(styletitle)
setCookie("mysheet", styletitle, days)
}
}

function indicateSelected(element){
//Optional function that shows which style sheet is currently selected
//within group of radio buttons or select menu
if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){
//if element is a radio button or select menu
var element=(element.type=="select-one") ? element.options : element
for (var i=0; i<element.length; i++){
if (element[i].value==selectedtitle){
//if match found between form element value and cookie value
if (element[i].tagName=="OPTION")
//if this is a select menu
element[i].selected=true
else
//else if it's a radio button
element[i].checked=true
break
}
}
}
}

if (manual_or_random=="manual"){
//IF MANUAL MODE
var selectedtitle=getCookie("mysheet")
if (document.getElementById && selectedtitle!=null)
//load user chosen style sheet from cookie if there is one stored
setStylesheet(selectedtitle)
}
else if (manual_or_random=="random"){
//IF AUTO RANDOM MODE
if (randomsetting=="eachtime")
setStylesheet("", "random")
else if (randomsetting=="sessiononly"){
//if "sessiononly" setting
if (getCookie("mysheet_s")==null)
//if "mysheet_s" session cookie is empty
document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/"
//activate random alt stylesheet while remembering its "title" value
else
setStylesheet(getCookie("mysheet_s"))
//just activate random alt stylesheet stored in cookie
}
else if (randomsetting.search(/^[1-9]+ days/i)!=-1){
//if "x days" setting
if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){
//if "mysheet_r" cookie
setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting))
//activate random alt stylesheet while remembering its "title" value
setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
}
else
setStylesheet(getCookie("mysheet_r"))
//just activate random alt stylesheet stored in cookie
}
}

if (document.images) {
pic2= new Image(1440,892);
pic2.src="/templates/transparent_bliss_1.5/images/sunset.jpg";
}
-->
</script>


<center>
<a href="javascript:chooseStyle('none',%2060)" checked="checked"><img src="thumb2.jpg" alt="background image 2" class="bgthumbs" border="0"></a>
<a href="javascript:chooseStyle('img1',%2060)"><img src="thumb1.jpg" alt="background image 1" class="bgthumbs" border="0"></a>
<a href="javascript:chooseStyle('img3',%2060)"><img src="thumb3.jpg" alt="background image 3" class="bgthumbs" border="0"></a>
<a href="javascript:chooseStyle('img4',%2060)"><img src="thumb4.jpg" alt="background image 4" class="bgthumbs" border="0"></a>
<a href="javascript:chooseStyle('img5',%2060)"><img src="thumb5.jpg" alt="background image 5" class="bgthumbs" border="0"></a>
<a href="javascript:chooseStyle('img6',%2060)"><img src="thumb6.jpg" alt="background image 6" class="bgthumbs" border="0"></a>
</center>

</center>




Random background does not work automatically
Posted
Updated 7-Jan-10 7:05am
v11

Um, What was the question?

If it's meant to be a tip or an article, you've posted in the wrong place!
 
Share this answer
 
sorry this code is still not able to function
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900