Preloading Images

If you did this example with the files on your desktop, you probably didn't notice any problem with the images loading. However, if you posted this file and the images on a server and some random person loaded the page and started playing with your rollovers, they would probably notice a small lag between the time they rolled over the image and when it was actually replaced. This happens because the browser hasn't loaded the rollover images (the ones ending in “_on.gif”) yet. In fact, it doesn't load those images until the user rolled over the image.

The way around this is to load the images into the browser's memory without showing those images to the user. Fortunately, JavaScript provides a relatively straightforward way to ...

Get Advanced JavaScript™: Insights and Innovative Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.