Forum Moderators: open

Message Too Old, No Replies

Preloading Images

Is there a better way?

         

pageoneresults

2:41 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm in the process of trimming out some code bloat on various sites and was wondering if there is a different way to preload images besides this...

<body onload="preloadImages();">

Can I preload through one of my external js or css files?

Rambo Tribble

2:59 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any preload must occur within the current page to be available on that page. The JS function that actually is called by the onload event handler can be in an external script. Alternatively, you can load the images into a hidden (through CSS properties) div and obtain the same effect.

Let me amend that, if the images are loaded in a previous page, they may still be available in cache. You cannot, however, rely on that.

john_k

4:11 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can move preloadImages (and any helper functions) into an external javascript file.

[edit]oops - I guess Rambo already said that.[/edit]

RonPK

6:36 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to get rid of the onload in the body tag, you can put something like this into your script:

window.onload = preloadImages;

Should work fine in an external script too.

pageoneresults

7:13 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RonPK, I think that is what I'm looking for. Should it be followed by "}"?

window.onload = preloadImages;
}
var preloadFlag = false;
function preloadImages() {
if (document.images) {Removed Specifics
}
}

Thanks to everyone else for your contributions. I was looking for the specific code since I am not fully versed in JavaScript. ;)

RonPK

7:48 am on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. The { and } are mostly used to group a couple of statements, and there is no need for that now.