Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2011, 06:03 AM   PM User | #1
joshuacos33
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
joshuacos33 is an unknown quantity at this point
Random background change with javascript

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>
joshuacos33 is offline   Reply With Quote
Old 09-19-2011, 04:47 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Old 09-19-2011, 05:00 PM   PM User | #3
joshuacos33
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
joshuacos33 is an unknown quantity at this point
<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 is offline   Reply With Quote
Old 09-20-2011, 08:15 PM   PM User | #4
joshuacos33
New to the CF scene

 
Join Date: Sep 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
joshuacos33 is an unknown quantity at this point
thanks for explanation.i didn't understand first after ypour explanation i got it.thank you
joshuacos33 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:39 PM.


Advertisement
Log in to turn off these ads.