0

Problem- At run time I am changing/replacing src attribute of some images using jquery. These images are by default hidden. I want to display these images when these images are download and ready to display because it is also possible that some images can not be downloaded.

         <img id="pic_1" width="153" height="160" border="0" 
                onmouseout="this.style.border='2px solid #FFFFFF';"
                   onmouseover="this.style.border='2px solid #4585E7';" 
                                       style="visibility: hidden;" 
                                        src="**to be replaced at run time**"">

Please let me know any solution how can i achieve this.

Freak
  • 6,786
  • 5
  • 36
  • 54
Gaurav Pant
  • 4,029
  • 6
  • 31
  • 54
  • You can get more ideas about this here: http://stackoverflow.com/questions/1977871/check-if-an-image-is-loaded-no-errors-in-javascript – Mike Drakoulelis Apr 25 '13 at 10:37

4 Answers4

2

I believe you can use the Load event for this.

Something like:

$('#myImage').load(function() {
  //called when image is loaded
});

Here is a working example

Here is an example with delayed loading

musefan
  • 47,875
  • 21
  • 135
  • 185
1
$('#imgId').load(function(){
alert('Image Loaded')
});
Raab
  • 34,778
  • 4
  • 50
  • 65
1

In addition to musefan's answer, if you need it to work with cached images as well, you'll need something more than load()

Check https://github.com/desandro/imagesloaded. It's a jQuery plugin that triggers a callback when images have been loaded. It works for cached images as well.

Check jQuery event for images loaded for more info.

Community
  • 1
  • 1
Osiris
  • 4,195
  • 2
  • 22
  • 52
0

This code would hook to every picture you got on your site with the class "preload".

$(function(){
    //Hide all pictures first (You could here work on some loading animations if you want)
    $("img.preload").css("display", "none");

    //When the image is loaded, show it again
    $("img.preload").load(function(){
        $(this).css("display", "block");
    });
});
2rsvold
  • 17
  • 2