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

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

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. Andrew Bewley via DotNetMonster.com

    window to window communication (window.opener)

    Andrew Bewley via DotNetMonster.com, Feb 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,298
    Andrew Bewley via DotNetMonster.com
    Feb 3, 2005
  2. Replies:
    5
    Views:
    2,210
    Andrew Thompson
    Jan 12, 2005
  3. defn noob
    Replies:
    4
    Views:
    735
    Carl Banks
    Jun 29, 2008
  4. Winfried Koenig

    IE window.parent.document.images problem

    Winfried Koenig, Dec 10, 2003, in forum: Javascript
    Replies:
    1
    Views:
    254
  5. Gilles T.
    Replies:
    1
    Views:
    319
    Jim Ley
    Jan 18, 2004
  6. Amir
    Replies:
    5
    Views:
    221
    Thomas 'PointedEars' Lahn
    May 29, 2004
  7. Peter
    Replies:
    2
    Views:
    235
    Peter
    Jun 13, 2006
  8. Isaac Won
    Replies:
    9
    Views:
    784
    Ulrich Eckhardt
    Mar 4, 2013
Loading...