Click to See Complete Forum and Search --> : Trying to set img src to array item
bhollin
09-30-2003, 01:43 AM
If I've defined an array in HEAD; e.g.,
<script>
var myImage=new Array("miles.gif","trane.gif","bird.gif");
</script>
and in BODY I have an anchor:
<a><img src="bird.gif"></a>
How would I instead refer to the array item instead of explicitly writing the url as shown here? i.e.,
<a><img src=myImage[2]></a>
and variations thereof (document.myImage[2], "myImage[2]", etc.) doesn't work.
Thanks.
try <img src="" onload="this.src=myImage[2]">
skriptor
09-30-2003, 03:16 AM
Hi,
with no real value for src no onload event is fired and with a src-value an endless loop is entered.
Try something like this:
<img src="empty.gif" onload="this.onload='return false;';this.src=myImage[1];">
Good luck, skriptor
bhollin
09-30-2003, 02:22 PM
Thanks. It works fine. Though I noticed that the
this.onload='return false;'
doesn't seem to do anything; i.e., it behaves the same without it. What's it supposed to do>
I have a similar issue trying to assign the contents of an array item into the iframe. For example, I want to replace the url in:
<iFrame src="myDoc.html"><iFrame>
with
frameDoc[i]
src=frameDoc[i] doesn't work, and the iFrame can't take an onload handler, so the technique used in the img won't work either. Recommendations?
skriptor
10-01-2003, 12:49 AM
Hi,
"return false" is used for netscape. IE do not need this.
iframe:
<html><head><script>
function init(){
document.getElementById( "i1" ).src="file.htm";
}
</script>
</head>
<body onload="init()">
<iframe id="i1" src="empty.htm"></iframe>
</body></html>
Good luck, skriptor