+ Reply to Thread
Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By descalzo
  • 1 Post By VPmase

Thread: Preloading images with Js - body onload or buttom of page?

  1. #1
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Preloading images with Js - body onload or buttom of page?

    Hi! I'm pretty new to the whole concept of preloading things with javascript, but it's really neat so I've started using it as much as I can.
    On the front page of my web strategy game I have thumbnails of game screenshots. To keep the user from waiting for the fullsize images when viewing the screenshots I preload them with Js when the user arrives at the page. See www.metropolisonline.org. (So when the screen shot is clicked, the full size image is already loaded.)

    Right now, the preload function runs on the bottom of the page. But then I thought, maybe this slows down the loading of other elements on the front page? Would it be better to put the preload function in the onload, ie <body onload="preloadscreenshotimages()", since this will load them AFTER all other information on the page? Or will other front page elements have priority since they come before the preload script on the page? Where would you prefer the preloading script to be executed?

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Preloading images with Js - body onload or buttom of page?

    Get Firefox, install Firebug and load/reload your front page after clearing your cache.

    Look at the 'Net' tab, 'All'. It will show you how the parts of your page load. I notice your vertical.png is not found (the current set up gives a 302 redirect to a regular system page named 404.shtml (ie you do not get an actual 404 error code).

    At least on Firefox, it seems that the CSS images are not requested until after the pre-loaded images (using the Javascript at the bottom of the page). So, it would seem better to change it to "onload" and let the CSS images render first.
    karimirt47 likes this.
    Nothing is always absolutely so.

  3. #3
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Re: Preloading images with Js - body onload or buttom of page?

    I do have Firebug already, but I've never used, or hardly noticed, the Net tab. Very handy for this kind of stuff. Thanks for the tip! Reminds me of the old saying
    "Give a man a fire and keep him warm for the rest of the day - set a man on fire and keep him warm for the rest of his life."

    Right now, I create an array of image objects, and assign each object src an image to preload. Do you know if I really need to do that? Or can I save memory by not assigning it to any variable:
    Code:
    new Image().src = 'image1.png'
    new Image().src = 'image2.png'
    I see Google's front page does it in one place, but I'm not really sure if it works as I hope. Or are browsers "too smart" and eliminates images never assigned to anything?

  4. #4
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: Preloading images with Js - body onload or buttom of page?

    Using a small variable and then clearing later doesn't hurt.

    http://www.pageresource.com/jscript/jpreload.htm
    dinomirt96 likes this.

  5. #5
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Preloading images with Js - body onload or buttom of page?

    It depends on the browser and how the images are being used. Firefox had, for a long time, a big disconnect between images used as <img> elements/JS .src values and images used in CSS -- the two caches never talked to one another, so if you were using the background-image property to display the images, preloading in JS wouldn't do a thing for you. (That, by the way, was the original reason for CSS sprites -- reducing the number of HTTP requests was a nice side-effect, but the fact that the browser wouldn't download a :hover state image until it was actually needed meant that CSS rollovers using images wouldn't really work until the second time you moused over something.)

    Preloads for <img> elements have worked pretty consistently -- the browser can't optimize them away unless the JS engine can see that the image variable is never going to be used, and since JS can be created dynamically, that's an almost impossible call to make.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. Javascript onload
    By driveflexfuel in forum Programming Help
    Replies: 7
    Last Post: 04-25-2010, 11:32 AM
  2. Images don't show up in page
    By mvastro in forum Programming Help
    Replies: 3
    Last Post: 01-22-2010, 09:49 PM
  3. Playsound onload
    By matty1980 in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 10-25-2009, 11:02 AM
  4. preloading image question
    By rickle42 in forum Graphics & Webdesign
    Replies: 7
    Last Post: 11-17-2007, 06:23 AM
  5. x10 buttom
    By dipeshsilwal1 in forum Scripts & 3rd Party Apps
    Replies: 12
    Last Post: 06-23-2005, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers