Preload Images. Please, help me out. Thank You.

Discussion in 'Javascript' started by shapper, Nov 25, 2006.

  1. shapper

    shapper Guest

    Hello,

    I think to preload an image I should us something like:

    img = new Image();
    img.src = 'images/img.jpg';

    Could someone tell me how to create a loop which would preload a list
    of images?
    Something like:

    ImagesFolder = '...';
    ImagesNames = '...';

    For i = 0 to ImagesNames.Count
    img = new Image();
    img.src = imagesFolder + imagesNames(i)
    End

    Is this a good approach?
    Could someone tell me the javascript code for this?
    I am not very confortable with javascript.

    Thank You,
    Miguel
     
    shapper, Nov 25, 2006
    #1
    1. Advertising

  2. shapper

    Jonas Raoni Guest

    shapper escreveu:
    > Is this a good approach?
    > Could someone tell me the javascript code for this?
    > I am not very confortable with javascript.


    Take a look if this helps: <URL:http://jsfromhell.com/classes/preloader>


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
     
    Jonas Raoni, Nov 25, 2006
    #2
    1. Advertising

  3. shapper

    mick white Guest

    shapper wrote:
    > Hello,
    >
    > I think to preload an image I should us something like:
    >
    > img = new Image();
    > img.src = 'images/img.jpg';
    >
    > Could someone tell me how to create a loop which would preload a list
    > of images?
    > Something like:
    >
    > ImagesFolder = '...';
    > ImagesNames = '...';
    >
    > For i = 0 to ImagesNames.Count
    > img = new Image();
    > img.src = imagesFolder + imagesNames(i)
    > End


    imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];
    for(var i=0;i<imgs.length;i++){
    pic=new Image()
    pic.src=imgs;
    }

    Something like that
    Mick
    >
    > Is this a good approach?
    > Could someone tell me the javascript code for this?
    > I am not very confortable with javascript.
    >
    > Thank You,
    > Miguel
    >
     
    mick white, Nov 25, 2006
    #3
  4. shapper

    webEater Guest

    On 25 Nov., 18:31, mick white <> wrote:
    > shapper wrote:
    > > Hello,

    >
    > > I think to preload an image I should us something like:

    >
    > > img = new Image();
    > > img.src = 'images/img.jpg';

    >
    > > Could someone tell me how to create a loop which would preload a list
    > > of images?
    > > Something like:

    >
    > > ImagesFolder = '...';
    > > ImagesNames = '...';

    >
    > > For i = 0 to ImagesNames.Count
    > > img = new Image();
    > > img.src = imagesFolder + imagesNames(i)
    > > Endimgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];

    > for(var i=0;i<imgs.length;i++){
    > pic=new Image()
    > pic.src=imgs;
    >
    > }Something like that
    > Mick
    >
    >
    >
    > > Is this a good approach?
    > > Could someone tell me the javascript code for this?
    > > I am not very confortable with javascript.

    >
    > > Thank You,
    > > Miguel


    Better create REAL images, means document.body.appendChild them to your
    site and simply set display="none" or visibility="hidden", so they are
    really preloaded. My experience with new Image() ... is bad, not all
    browser preload them onload. Example

    imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];
    for(var i=0;i<imgs.length;i++){
    // why make an image array, excepting you want get the images
    explicitly. Important is, that the browser loads the image data.
    var im = document.createElement('img'); // probably the same as new
    Image(), I dont know.
    im.src=imgs;
    im.style.width = im.style.height = '0';
    im.style.visibility = 'hidden';
    // I am not sure if your browser loads them using display="none"
    }

    Andi
     
    webEater, Nov 25, 2006
    #4
  5. shapper

    ASM Guest

    shapper a écrit :
    > I think to preload an image I should us something like:
    >
    > img = new Image();
    > img.src = 'images/img.jpg';
    >
    > Could someone tell me how to create a loop which would preload a list
    > of images?
    > Something like:
    >
    > ImagesFolder = '...';
    > ImagesNames = '...';
    >
    > For i = 0 to ImagesNames.Count
    > img = new Image();
    > img.src = imagesFolder + imagesNames(i)
    > End


    imgs=["a.gif","../b.gif","http://example.com/imgs/c.gif" ...],pics=[];

    function postLoad(k, max){
    if(k<max) {
    pic[k] = new Image();
    pic[k].onload = function(){postLoad(k,max)};
    pic[k].src = imgs;
    k++;
    }
    }

    onload = function(){ postLoad(0, imgs.length); };


    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
    Contact : http://stephane.moriaux.perso.wanadoo.fr/contact
    ASM = Aimable Stéphane Moriaux = Amateur Sasseur Merdouilles
     
    ASM, Nov 26, 2006
    #5
    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. Miguel Dias Moura
    Replies:
    1
    Views:
    401
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 1, 2004
  2. Miguel Dias Moura
    Replies:
    4
    Views:
    2,268
  3. Replies:
    27
    Views:
    775
    Andy Dingley
    Oct 31, 2007
  4. PETER WONG F H (+971 50 8320722)

    Please send you property detail to me,thank you !

    PETER WONG F H (+971 50 8320722), Mar 14, 2010, in forum: Python
    Replies:
    0
    Views:
    319
    PETER WONG F H (+971 50 8320722)
    Mar 14, 2010
  5. Replies:
    29
    Views:
    352
    Andy Dingley
    Oct 31, 2007
Loading...

Share This Page