Caching images

S

Samik R.

Zeba said:
Hi!

The CastleRater javascript code ( available at
http://blogs.crsw.com/mark/archive/2005/01/30/719.aspx) for rating
control has code similar to this ...

function CastleRater(id, imageOnUrl, imageOffUrl, imageHoverUrl,
maxValue, value)
{
.
.

this.ImageOn = imageOnUrl;
.
.

new Image().src = this.ImageOn;
.
.
}

What does this last line actually do ??!! They've said it's used to
cache the images, but how does that happen ?

Thanks !!
When the JS processor comes across new Image().src="...", it reads the source and stores the image in the memory, which is same as caching. Very helpful to toggle images, rollover etc., although a better way to do this is to use CSS.
 
Z

Zeba

Hi !
When the JS processor comes across new Image().src="...", it reads the source and stores the image >in the memory, which is same as caching. Very helpful to toggle images, rollover etc.,

Okay, so new Image() creates a new Image whose src is as specified. So
how is this stored - I mean, how can you actually reference this image
when you want it ?
although a better way to do this is to use CSS.
How could we implement image caching using CSS ?

Thanks !!
 
P

Pete

Okay, so new Image() creates a new Image whose src is as specified. So
how is this stored - I mean, how can you actually reference this image
when you want it ?

here is some code that will preload the image, and on completion of
loading, will append the image to DOM.

<html>
<head></head>

<script>

img = null;
loadImage = function() {
img = new Image();
img.onload = function() {
document.getElementById('holder').appendChild(img);
};
img.src = "test.jpg";
}



window.onload = loadImage;
</script>

<body>

<div id="holder"></div>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top