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>'+' ');
document.write('<a ' + 'href="javascript:swap(0)">'
+ '<em>'+ carstages[0] + '<\/em>'+ '<\/a>'+' ');
document.write('<a ' + 'href="javascript:swap(1)">'
+ '<em>'+ carstages[1] + '<\/em>' + '<\/a>'+' ');
document.write('<a ' + 'href="javascript:swap(2)">'
+ '<em>'+ carstages[2] + '<\/em>' + '<\/a>'+' ');
document.write('<a ' + 'href="javascript:swap(3)">'
+ '<em>'+ carstages[3] + '<\/em>' + '<\/a>'+' ');
document.write('<a ' + 'href="javascript:swap(4)">'
+ '<em>'+ carstages[4] + '<\/em>' + '<\/a>'+' ');
</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>'+' ');
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>'+' ');
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: why doesn't wprintf work - comp.unix.programmerwhy doesn't wprintf work - comp.unix.programmer ... standard if the thread doesn't ... Why is that ??? Is there any work around to this problem ??? ... id, NULL, do_loop ... if statements to change indexing - comp.soft-sys.matlabi have code that works if i make an index ... the indexing, you should try a while loop. But why don't ... under the assumption it is inside a for loop, it likely doesn't ... clock microseconds with resolution in milliseconds - comp.lang.tcl ...I would like some kind of busy waiting 'for' loop. But I fear it won't be very hardware independent, and may not work on different kind of PCs. enum and operator++ - comp.lang.c++... return dd; } void f() { for (Dir d = North; d < Max_; ++d) // do something } The problem is: in the above loop the ++ doesn't work, indeed the loop is ... Invisible window, like console app - comp.os.ms-windows.programmer ...... really worth getting a book like the Petzold one if you are not familiar with the basics of Win32 message loops etc.. None of it is complex but it also doesn't work how ... Subscript indices must either be real positive integers or ...Hi, I have created a code which works fine for i=1, i=2, i=3 etc but if I have it done in a loop (e.g ... works end for i=1:2 <code> ----> DOESN'T work!!!! WHY ... Trapezoidal Rule in MATLAB. - comp.soft-sys.matlab... on it's own, but the sum function doesn't ... appreciated. > > Michael sum works on a vector or matrix, all your values are scalars. (hint: don't need the for loop) Infinite Loop in Code -- Logic Error - comp.lang.rubyenum and operator++ - comp.lang.c++... doesn't work, indeed the loop is infinite ... My code had two problems. Yu found one of them and you found the wraping one. onbeforeunload doesn't work in Safari - comp.lang.javascript ...Any reason why window.onunload won't work? Maybe use both, and have onbeforeunload set a flag that onunload checks for to work around things like the Google toolbar that ... instead of For loop - comp.soft-sys.matlab> Why don't you post the actual FOR loops instead of the ... That is why I requested the actual code. If the OP *doesn't* get ... mex code will work, or maybe ... ??? You haven't ... java - Why doesn't this for-each loop work? - Stack OverflowThe for-each loop will not work for this case. You cannot use a for-each loop to initialize an array. Your code: int[] array = new int[5]; for (int i : array) { i ... Why won't this for loop work? (Beginning Java forum at JavaRanch)This is supposed to be simple coloring book program, but I can't find out why it is giving me errors. [code] package CustomPaint; import java.awt 7/23/2012 12:55:11 AM
|