See this method:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<script>
$(function() {
$("img.deferred").each(function(index) {
var tochange = this;
var altsrc = $(this).attr('altsrc');
var img = $("<img />").attr('src', altsrc).load(function() {
$(tochange).attr('src', altsrc)
});
});
});
</script>
Sample usage:<br>
<img class="deferred" src="http://www.nmh.ie/iopen24/images/content_images/other/warning.jpg" altsrc="http://www.jeremynoeljohnson.com/jimages/blogposts/apples01.jpg"><br>
<img class="deferred" src="http://www.nmh.ie/iopen24/images/content_images/other/warning.jpg" altsrc="http://www.jeremynoeljohnson.com/jimages/blogposts/NOTFOUND.jpg">
Notice the two
img
elements above. Both have a specific class, this makes them different from all others, and that is used by the jquery selector to find only them. The
src
attribute holds an url of a dummy static image, that is loaded by default. The
altsrc
(non-standard, custom) attribute holds the actual image url. The second sample element has this attribute set to a non-existent url. On page load all deferred img elements are parsed. The script tries to load the actual image file in a dummy element. If it succeeds, transfers the value to the actual image tag.
Hope it helps you.