Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Making window.document.images[] work in a loop

Reply
Thread Tools

Making window.document.images[] work in a loop

 
 
extremerep@yahoo.com
Guest
Posts: n/a
 
      07-06-2006
I am trying to have
"window.document.images[i].src=''+cards1[i]+'.gif'" work in a loop just
as "document.write(<img src="'+cards[i]+'">); does, but it does not
seem to be within Javascript's ability.

The following works:

for(var i=1;i<6;i++){

document.write('<td><img src="'+cards[i]+'.gif"></td>');

}

but this one does not:

for(var i=1;i<6;i++){

window.document.images[i].src=''+cards1[i]+'.gif";

}

Any help is greatly appreciated,
Thanks.

 
Reply With Quote
 
 
 
 
Vincent van Beveren
Guest
Posts: n/a
 
      07-06-2006
Why would you want to do that. The document.images array is an array of
elements that are _already_ on the page.

If you which to pre-load graphics you can just create new image objects:

for (var i= 1 ; i < 6; i++) {
img = new Image();
img.src = cards1[i] + '.gif';
}

Or, you can put them in an array:

var cardImgs = new Array(6);
for (var i= 1 ; i < 6; i++) {
cardImgs[i] = new Image();
cardImgs[i].src = cards1[i] + '.gif';
}

Or you can add them to a page by using DOM (in the onload):

document.onload = function() {

for (var i= 1 ; i < 6; i++) {
var myImg = document.createElement('img');
myImg.src = cards1[i] + '.gif';
document.body.appendChild(myImg);
}
}

Good luck,
Vincent



wrote:
> I am trying to have
> "window.document.images[i].src=''+cards1[i]+'.gif'" work in a loop just
> as "document.write(<img src="'+cards[i]+'">); does, but it does not
> seem to be within Javascript's ability.
>
> The following works:
>
> for(var i=1;i<6;i++){
>
> document.write('<td><img src="'+cards[i]+'.gif"></td>');
>
> }
>
> but this one does not:
>
> for(var i=1;i<6;i++){
>
> window.document.images[i].src=''+cards1[i]+'.gif";
>
> }
>
> Any help is greatly appreciated,
> Thanks.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Making a loop hesitate. snewcrash Java 9 12-05-2005 11:49 PM
Re: A fresh new approach to making making online Fakename Computer Information 0 11-18-2005 02:10 AM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM
Making new Flavors : Making a custom transferhandler for and drop applications ebby83@gmail.com Java 5 01-12-2005 11:10 AM



Advertisments
 



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