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
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Assigning parameters for functions, and looping

    I am a beginner of JavaScript, and am trying to work out a code through which the following action can take place:
    There is a frame, the top part of the frame contains a button. On clicking the button, the bottom part of the frame changes colors, which are contained in an array present in the head of the code.
    For some reason, this is not working, perhaps something wrong with the way I have assigned the parameters.
    Can anybody help?
    Here is the required part of the code:

    <html>
    <head>
    <script language="Javascript">
    <!--
    var colory = new Array ("red","blue","green","yellow","orange");
    var loop ="";

    function fixLoopy()
    {

    for(var loop = 0; loop<colory.length; loop+=1)

    for (var loop = 0; loop > colory.length; loop = 0)
    { return loop;
    }

    }
    //-->
    </script>

    </head>

    And in the body section,

    <a href="#" onClick="fixLoopy(); top.bottom_target.document.bgColor= colory[loop]; return false;">
    <img src="rainbow.jpg" width="46" height="40"> </a>

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Try it this way

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>Document Title</TITLE>

    <
    script language="Javascript">
    <!--
    var 
    colory = new Array ("red","blue","green","yellow","orange");

    var 
    loop =0

    function fixLoopy(){

    parent.bottom_target.document.body.style.backgroundColor=colory[loop]

    loop++

    if(
    loop==colory.length){
    loop=0
    }

    setTimeout("fixLoopy()",1000)
    }

    //-->
    </script>

    </HEAD>
    <BODY>

    <a href="#" onClick="fixLoopy(); return false"> 
    <img src="rainbow.jpg" width="46" height="40"> </a>
    </BODY>
    </HTML> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    thanks so much! it was succesful!! i just had to remove the "timeout" line because i just wanted the colour to change one-by-one on clicking... anyway thanks a lot :-)

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    a little bit shorter:
    PHP Code:
    <script type="text/javascript">
    var 
    colory = ['red','blue','green','yellow','orange']; 
    var 
    loop=0;
    function 
    fixLoopy(){
    loop=loop==colory.length?0:loop;
    parent.bottom_target.document.body.style.backgroundColor=colory[loop];
    loop++;
    }
    </script> 
    PHP Code:
    <img src="rainbow.jpg" width="46" height="40" onclick="fixLoopy()" style="cursor:pointer"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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