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

E

extremerep

I am trying to have
"window.document.images.src=''+cards1+'.gif'" work in a loop just
as "document.write(<img src="'+cards+'">); 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+'.gif"></td>');

}

but this one does not:

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

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

}

Any help is greatly appreciated,
Thanks.
 
V

Vincent van Beveren

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 + '.gif';
}

Or, you can put them in an array:

var cardImgs = new Array(6);
for (var i= 1 ; i < 6; i++) {
cardImgs = new Image();
cardImgs.src = cards1 + '.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 + '.gif';
document.body.appendChild(myImg);
}
}

Good luck,
Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top