SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Help with image preloader

    Can someone help me figure out why these preloaders aren't working?
    Its for the buttons on the right. The function is getting called, but it doesn't seem to be even trying to load the images according to Firebug.

    http://s181455844.onlinehome.us/tk/
    Code JavaScript:
    function preloader() 
     
    {
     
      // counter
         var i = 0;
     
         // create object
         imageObj = new Image();
     
     
         // set image list
         images = new Array();
         images[0]="images/display/buttons/volunteer_o.png"
         images[1]="images/display/buttons/contribute_o.png"
         images[2]="images/display/buttons/Host_o.png"
         images[3]="images/display/buttons/Order_o.png"
         images[4]="images/display/buttons/Contact_o.png"
         images[5]="images/display/buttons/Endorsements_o.png"
     
     
         // start preloading
         for(i=0; i<=3; i++) 
         {
              imageObj.src=images[i];
         }
     
     
     
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    First, the preloading for loop only goes up to 3 when 5 is the last image.

    Try this code instead:

    Code javascript:
    // start preloading
    for(i=0; images[i]; i++) 
    {
        imageObj.src=images[i];
    }

    The script isn't preloading because the onload attribute in the body is waiting until the page has fully completed loading, images and all, before it runs the script.

    It's a bit late by then.

    Move the preloader() statement to the end of the script, and get rid of that onload attribute.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    First, the preloading for loop only goes up to 3 when 5 is the last image.
    God 95% of the time its something stupid.
    Man Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •