Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,084
    Thanks
    23
    Thanked 593 Times in 592 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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>

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for explanation.i didn't understand first after ypour explanation i got it.thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •