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

Discussion in 'Javascript' started by extremerep@yahoo.com, Jul 6, 2006.

  1. Guest

    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.
     
    , Jul 6, 2006
    #1
    1. Advertising

  2. 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



    wrote:
    > 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.
    >
     
    Vincent van Beveren, Jul 6, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. defn noob
    Replies:
    4
    Views:
    622
    Carl Banks
    Jun 29, 2008
  2. Winfried Koenig

    IE window.parent.document.images problem

    Winfried Koenig, Dec 10, 2003, in forum: Javascript
    Replies:
    1
    Views:
    201
  3. Amir
    Replies:
    5
    Views:
    106
    Thomas 'PointedEars' Lahn
    May 29, 2004
  4. Peter
    Replies:
    2
    Views:
    174
    Peter
    Jun 13, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page