Preloading and JavaScript Image() Objects
A lot of high-res images can really make a website cleaner. But they can also make your site slower - images are files, files use bandwidth, and bandwidth is directly related to latency. It's time to learn how to speed up your Web site through a technique called image preloading.
Image preloading
For browser to load images, they will only be loaded by the browser after sending an HTTP request for the image. The HTTP request for the image is either Use the tag, or via a method call. If you use a JavaScript script to handle swapping images on mouseover events, or automatically changing images after a period of time, you may have to wait from a few seconds to a few minutes while getting the image from the server. This is especially true if you're using a slow Internet connection, or the image you're trying to retrieve is very large, or some other situation; in this case, the delay will prevent you from achieving the results you expect.
Some browsers take some measures to alleviate this problem, such as trying to store the image in the local cache so that subsequent calls to the image can be satisfied immediately; however, there will still be some problems when the image is first called. Delay. Preloading is a method of downloading images to cache before they are needed. This way, when the image is actually needed, it can be fetched from the cache and displayed immediately.
Image() object
The easiest way to preload an image is to instantiate a new Image() object in JavaScript, and then pass in the URL of the image that needs to be loaded as a parameter . Suppose we have an image called heavyimagefile.jpg, and we want to display this image when the user's mouse is placed over an already displayed image. To preload this image for faster response times, we simply create an Image() object heavyImage and load it simultaneously in the onLoad() event handler.