martedì 21 settembre 2010

Image prefetch with javascript

In order to perform image prefetching with javascript we need to create an Image object and insert an url in its 'src' global variable, as follows:

var img = new Image()
img.src = 'http://my_url.jpg'


Then we can attach handlers in order to be advised when fetching operation completes or when it goes wrong:

img.onload = function() {
// remove placeholder and show image
}

img.onerror = function() {
// remove placeholder and print that image loading went wrong
}


That's all, enjoy