Why doesn't this for loop work?

  • Follow


I have a sequence of four document.write commands that work. The
swap() function swaps in a new picture and the carstages array is a
list of what the picture represents. So you under the picture you geth
a line of text links links on which to click to see the next picture.
And it all works (Yay!!).

But here's my question-- when I comment out the four individual
instructions and replace them with the loop, I get a picture, but I
don't get the line of links underneath anymore to click on. Probably
something stupid, but I never claimed to be an expert. :-(

Any suggestions? Here's the code (this is in the body section):

    <script>type="text/javascript">

// problem with this loop:
//    for(j = 0; j < carstages.length; j++)
//        document.write('<a ' + 'href="javascript:swap(j)">'
//        +  '<em>'+ carstages[j]  + '<\/em>'+ '<\/a>'+'&nbsp');
   
        document.write('<a ' + 'href="javascript:swap(0)">'
            +  '<em>'+ carstages[0]  + '<\/em>'+ '<\/a>'+'&nbsp');

        document.write('<a ' + 'href="javascript:swap(1)">'
            +  '<em>'+ carstages[1] + '<\/em>' + '<\/a>'+'&nbsp');

        document.write('<a ' + 'href="javascript:swap(2)">'
            +  '<em>'+ carstages[2] + '<\/em>' + '<\/a>'+'&nbsp');

        document.write('<a ' + 'href="javascript:swap(3)">'
            +  '<em>'+ carstages[3] + '<\/em>' + '<\/a>'+'&nbsp');

        document.write('<a ' + 'href="javascript:swap(4)">'
            +  '<em>'+ carstages[4] + '<\/em>' + '<\/a>'+'&nbsp');
   </script>

The swap function is in the header:

function swap(imgIndex) 
  }
    document['imgMain'].src = carimages[imgIndex];
  }

and the carstages array is just an array of strings.

Thanks.

--Lynn
0
Reply Mr 10/28/2007 11:39:44 PM

On Oct 29, 9:39 am, "[Mr.] Lynn Kurtz" <ku...@asu.edu.invalid> wrote:
> I have a sequence of four document.write commands that work. The
> swap() function swaps in a new picture and the carstages array is a
> list of what the picture represents. So you under the picture you geth
> a line of text links links on which to click to see the next picture.
> And it all works (Yay!!).
>
> But here's my question-- when I comment out the four individual
> instructions and replace them with the loop, I get a picture, but I
> don't get the line of links underneath anymore to click on. Probably
> something stupid, but I never claimed to be an expert. :-(
>
> Any suggestions? Here's the code (this is in the body section):
>
>     <script>type="text/javascript">
>
> // problem with this loop:
> //    for(j = 0; j < carstages.length; j++)

The body of a for loop is supposed to be enclosed in {} (curley
braces).  You can omit the braces if the body is only one statement,
but for more than one statement you must use braces:

      for(j = 0; j < carstages.length; j++) {
        /* stuff here */
      }

Your loop body is only one statement, however it is long so I'd
suggest using braces.  It makes maintenance that much easier.


> //        document.write('<a ' + 'href="javascript:swap(j)">'
> //        +  '<em>'+ carstages[j]  + '<\/em>'+ '<\/a>'+'&nbsp');

You have not use "var" to make the scope of j local, so it is global.
When the function runs the first time, it loops until j is set to
carstages.length, whereupon it ends.  When you later call swap(j), all
the functions reference this last value of j.  Since
carstages[carstages.length] never exists (the length is always one
greater than the highest index), you get an error.

I suggest that you don't use javascript within the href attribute, and
instead set it to something useful.  Then use an onclick handler to
grab the (now useful) href attribute to swap the image and, if
successful, return false.  Or put the index into the ID of the a
element and use that, e.g..

  document.write('<a ' + 'href="' + carstages[j] + '" id="img_' + j +
'"
    + 'onclick="swap(this.id);return false;">'
    + '<em>'+ carstages[0]  + '<\/em>'+ '<\/a>'+'&nbsp');

Untested, but you should get the idea.

[...]
>
> The swap function is in the header:
>
> function swap(imgIndex)
>   }

Given that you are now passing an ID like "img_0" you can use
something like:

      var imgIndex = imgIndex.split('_')[1];


>     document['imgMain'].src = carimages[imgIndex];
>   }
>
> and the carstages array is just an array of strings.


--
Rob

0
Reply RobG 10/29/2007 12:29:30 AM


[Mr.] Lynn Kurtz meinte:
> I have a sequence of four document.write commands that work. The
> swap() function swaps in a new picture and the carstages array is a
> list of what the picture represents. So you under the picture you geth
> a line of text links links on which to click to see the next picture.
> And it all works (Yay!!).
> 
> But here's my question-- when I comment out the four individual
> instructions and replace them with the loop, I get a picture, but I
> don't get the line of links underneath anymore to click on. Probably
> something stupid, but I never claimed to be an expert. :-(
> 
> Any suggestions? Here's the code (this is in the body section):

>     <script>type="text/javascript">

<script text...>

> // problem with this loop:
> //    for(j = 0; j < carstages.length; j++)
> //        document.write('<a ' + 'href="javascript:swap(j)">'

...."javascript:swap('+j.toString()+')"...

There are various other weaknesses in your script (click handler instead 
of pseudo-protocol, scopes of variables), but this seems to be the core 
error.

Gregor



-- 
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at  ::: Forum f�r Multivisionsvortr�ge
http://www.image2d.com     ::: Bildagentur f�r den alpinen Raum
0
Reply Gregor 10/29/2007 12:29:56 AM

Gregor Kofler meinte:

>> // problem with this loop:
>> //    for(j = 0; j < carstages.length; j++)
>> //        document.write('<a ' + 'href="javascript:swap(j)">'
> 
> ..."javascript:swap('+j.toString()+')"...
> 
> There are various other weaknesses in your script (click handler instead 
> of pseudo-protocol, scopes of variables), but this seems to be the core 
> error.

Together with the alredy mentioned curly braces.

Gregor


-- 
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at  ::: Forum f�r Multivisionsvortr�ge
http://www.image2d.com     ::: Bildagentur f�r den alpinen Raum
0
Reply Gregor 10/29/2007 12:32:06 AM

Gregor Kofler wrote:
>>     <script>type="text/javascript">
> 
> <script text...>

  <script type="text/javascript">

>> // problem with this loop:
>> //    for(j = 0; j < carstages.length; j++)
>> //        document.write('<a ' + 'href="javascript:swap(j)">'
> 
> ..."javascript:swap('+j.toString()+')"...

Calling .toString() is unnecessary as the concatenation operation will call
that implicitly already.

  ..."javascript:swap('+j+')"...

> There are various other weaknesses in your script (click handler instead 
> of pseudo-protocol,

You mean the weakness of using a pseudo-protocol instead of a click handler,
not vice-versa.

> scopes of variables), but this seems to be the core error.

ACK


Regards,

Pointed"Goodnight? ;-)"Ears
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
0
Reply Thomas 10/29/2007 12:37:08 AM

On Mon, 29 Oct 2007 01:37:08 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Gregor Kofler wrote:
>>>     <script>type="text/javascript">
>> 
>> <script text...>
>
>  <script type="text/javascript">

Ahh, yes, thanks.

>
>>> // problem with this loop:
>>> //    for(j = 0; j < carstages.length; j++)
>>> //        document.write('<a ' + 'href="javascript:swap(j)">'
>> 
>> ..."javascript:swap('+j.toString()+')"...
>
>Calling .toString() is unnecessary as the concatenation operation will call
>that implicitly already.
>
>  ..."javascript:swap('+j+')"...
>

And thanks again..

>> There are various other weaknesses in your script (click handler instead 
>> of pseudo-protocol,
>
>You mean the weakness of using a pseudo-protocol instead of a click handler,
>not vice-versa.
>
>> scopes of variables), but this seems to be the core error.

Thanks to all who replied. I have the loop working now. Next step is
to read about click handlers I guess. Great group here.

Cheers.

--Lynn
0
Reply Mr 10/29/2007 1:29:49 AM

5 Replies
66 Views

(page loaded in 0.131 seconds)

Similiar Articles:













7/23/2012 12:55:11 AM


Reply: