...

View Full Version : Random background change with javascript



joshuacos33
09-19-2011, 07:03 AM
can you explain someone step by this code?i didn't get it.

<script language="Javascript">



<!--





function backcolor(form){

temp = ""

for (var i = 0; i < 16; i++) {

temp = form.color[i].value

if (form.color[i].checked){ document.bgColor = temp }

}

}



function randombackground(){



document.bgColor = getColor()



}



function getColor(){



currentdate = new Date()

backgroundcolor = currentdate.getSeconds()



if (backgroundcolor > 44)

backgroundcolor = backgroundcolor - 45

else if (backgroundcolor > 29)

backgroundcolor = backgroundcolor - 30

else if (backgroundcolor > 15)

backgroundcolor = backgroundcolor - 16



if (backgroundcolor == 0 )

return "olive";

else if (backgroundcolor == 1 )

return "teal";

else if (backgroundcolor == 2 )

return "red";

else if (backgroundcolor == 3 )

return "blue";

else if (backgroundcolor == 4 )

return "maroon";

else if (backgroundcolor == 5 )

return "navy";

else if (backgroundcolor == 6 )

return "lime";

else if (backgroundcolor == 7 )

return "fuschia";

else if (backgroundcolor == 8 )

return "green";

else if (backgroundcolor == 9 )

return "purple";

else if (backgroundcolor == 10 )

return "gray";

else if (backgroundcolor == 11 )

return "yellow";

else if (backgroundcolor == 12 )

return "aqua";

else if (backgroundcolor == 13 )

return "black";

else if (backgroundcolor == 14 )

return "white";

else if (backgroundcolor == 15 )

return "silver";

}



// -->



</script>

sunfighter
09-19-2011, 05:47 PM
Your javascript consists of three functions = backcolor, randombackground, and getColor.

Looking at backcolor is seems your main html body has a form with 16 RADIO buttons that have a color associated with each one. (I bet they are identical to the numbers and colors listed in function getColor.) When one is checked and the form submit button is clicked this determines your BGcolor by what you checked.

You must also have a RANDOM button that calls randombackground. This sets the BGcolor to the return value of getColor.

getColor uses the seconds of the current time and then uses if statements to get that number(which can be 0 to 59) between 0 and 15. It then goes into some If/Else statements that pick the BGcolor.

joshuacos33
09-19-2011, 06:00 PM
<script language="Javascript">



<!--





function backcolor(form){

temp = ""

for (var i = 0; i < 16; i++) {

temp = form.color[i].value

if (form.color[i].checked){ document.bgColor = temp }

}

}



function randombackground(){



document.bgColor = getColor()



}



function getColor(){



currentdate = new Date() -------- Why date object created for background?

backgroundcolor = currentdate.getSeconds()



if (backgroundcolor > 44) --------why it start with 44?or why 44?
backgroundcolor = backgroundcolor - 45

else if (backgroundcolor > 29)

backgroundcolor = backgroundcolor - 30

else if (backgroundcolor > 15)

backgroundcolor = backgroundcolor - 16



if (backgroundcolor == 0 )

return "olive";

else if (backgroundcolor == 1 )

return "teal";

else if (backgroundcolor == 2 )

return "red";

else if (backgroundcolor == 3 )

return "blue";

else if (backgroundcolor == 4 )

return "maroon";

else if (backgroundcolor == 5 )

return "navy";

else if (backgroundcolor == 6 )

return "lime";

else if (backgroundcolor == 7 )

return "fuschia";

else if (backgroundcolor == 8 )

return "green";

else if (backgroundcolor == 9 )

return "purple";

else if (backgroundcolor == 10 )

return "gray";

else if (backgroundcolor == 11 )

return "yellow";

else if (backgroundcolor == 12 )

return "aqua";

else if (backgroundcolor == 13 )

return "black";

else if (backgroundcolor == 14 )

return "white";

else if (backgroundcolor == 15 )

return "silver";

}



// -->



</script>

joshuacos33
09-20-2011, 09:15 PM
thanks for explanation.i didn't understand first after ypour explanation i got it.thank you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum