Javascript img declaration loop

Ask a Question related to Macromedia Dynamic HTML, Design and Development.

  1. #1

    Default Javascript img declaration loop

    Is it possible to create a loop to create a series of new images, rather than
    explicitly declaring each. So, rather than the following two lines repeated 75
    times for grid1 through grid75 ...

    grid1 = new Image();
    grid1.src = "images/nav/grid.png";


    is it possible instead to do something like this:

    for (var i = 1; i < 75; ++i)
    {
    var myGrid = eval("grid" + i);
    myGrid = new Image();
    myGrid.src = "images/nav/grid.png";
    }

    The above code isn't working (or I wouldn't be posting :) ... ) but I'm
    unclear whether the concept is flawed or I just haven't gotten the syntax
    right. I've tried a handful of other syntax constructions, but they haven't
    worked either.

    Thanks,

    Andy

    Andigo Guest

  2. Similar Questions and Discussions

    1. javascript array and loop question
      I'm passing in a comma delimited list as a parameter to a function. Ex. the argument passed in holds the following values: 1, 2, 3, 4, 5, I need...
    2. Can a film loop play once, then loop on the last frame(s)?
      I need a film loop to play once, then loop playback on the last frame so I can keep the LOOP of the film loop set. This will allow the tell commands...
    3. Film loop rollovers working with tell sprite, but only if Loop is checked
      on mouseWithin me cursor 280 tell sprite 40 --the sprite containing the film loop sprite(60).member = member("networkmapsbuttonroll") --swapping...
    4. Urgent: Repeat loop and Film loop clash!
      Hi All, Scenario I have a script running in which the spelling which was typed in by the student is corrected. The alphabets are moved to...
    5. loop in javascript
      Hi! I have a menu-system in JavaScript. The menu is displayed by two js functions ( DrawMenuBar and DrawSubMenu ). the first function displays...
  3. #2

    Default Re: Javascript img declaration loop

    The code you posted worked. The problem was the name of the var,
    that was always the same. Using that way, you will be creting 75 times
    the same variable. I'll need to use an array or declare variables with
    diferent names (using eval).

    Example with array:
    var j,a=new Array();
    for (j=0; j<75; j++) {
    a[j]=new Image(); a[j].src='images/nav/grid.png';
    } //Creates 75 index of the array "a" (0-74).

    Example with eval:
    var j,tmpV;
    for (j=1; j<=75; j++) { varAtual=eval('variabelNumber'+(j+1));
    varAtual=new Image(); varAtual.src='images/nav/grid.png';
    } //Creates 75 variables with names "varNum1", "varNum2"... till
    "varNum75".


    []s


    --
    - Rio de Janeiro, Brasil
    - Troyan <dnunes[at]email[dot]com>


    Troyan Guest

  4. #3

    Default Re: Javascript img declaration loop

    Thank you very much. I'll give it a try. Regards, Andy
    Andigo Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139