Technology Programming

JavaScript By Example

Directly setting the src attribute of an image tag to an image that has not been previously downloaded will result in that image needing to be downloaded prior to that update being able to be applied. So with the code in our prior example the first time thescript is run the summer image will be displayed while the image for the correct season is being downloaded. There isn't much we can do about that delay in that situation as the script starts downloading the correct image at the earliest possible moment.

There are many instances though where we know in advance that we want to update the page to display certsin images (or at least that they are likely to be required). Where we know that an image will be required later we can actually use JavaScript to request it to be downloaded in advance without the downloaded image being added to the page straight away.

In this example we request for all four season images to be downloaded into the browser cache even though only one of them is displayed in the page. This will allow us to add further code to the page (not shown) that then cycles through all the images without any delay the first time each image is requested (assuming that sufficient time elapses to download the images prior to our requesting them). Even if an image is still being downloaded when the additional code to add it into the web page is called it will still update faster than if we don't use the preload code because the image will at least have already started downloading in advance.

HTML


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Example D13</title>
</head>
<body>
<img id="season" src="summer.gif" alt="season">
<script type="text/javascript" src="exampleD13.js"></script>
</body>
</html>

JavaScript


var backImg, img;
backImg = ['summer.gif','autumn.gif','winter.gif','spring.gif'];
img = []; for (var i = backImg.length; i >= 0; i--) {
img[i] = new Image();
img[i].src = backImg[i];
}

Related posts "Technology : Programming"

Differences Between Byte Array Vs. String

Programming

Web Design Company UK/

Programming

Develop A Quality Plan With These Self Help Tips

Programming

TMediaPlayer: What track am I on?

Programming

How You Can Prove Your Expertise

Programming

Furniture Advice And Tips And Also Hardwearing. Property Searching Wonderful

Programming

Web Design Fort Lauderdale Business

Programming

Hire Web Designer and Diminish Development Expenditure

Programming

Web Masters.

Programming

Leave a Comment