tags:

views:

484

answers:

3

I recently asked how to detect when all resources of a page have been loaded, such as images and stylesheets. The answer came back, use the $(window).load(); method in jQuery.

My question now is how do I detect when content is done loading via AJAX. AJAX injects some img elements, say, into the DOM... how can I tell when those images have finished loading?

+2  A: 

In the $.get() or $.post() function, you can supply the name of a function that will be called when the ajax request has completed. For example:

$.get("something.php",{data:'test'},foo);

function foo()
{
   // This function will be called when the request has completed
}

Or:

$.get("something.php",{data:'test'},function()
  {
    //This function will be called in the same way
  }

);
Click Upvote
I don't need to know when the AJAX request is complete, because the AJAX request will return HTML, and then the browser will go fetch the images.
Antony Carthy
I can be wrong, but if you know of the height and width of the image expected (perhaps it can be returned in the html/JSON returned by server), in the callback function you can do a while loop which checks if $("#img").attr("height") or such matches the expected height. When it does it could mean that the image has loaded fully.
Click Upvote
Unfortunately that isn't dynamic enough, as this is user content which can come in at any size...
Antony Carthy
there are built in PHP functions which can return the height and width of the image. You can use them to get the height and width, and supply that with the url to the image in a JSON array (look at php.net/json_encode). I'm sure there are such functions in other langs too.
Click Upvote
How does firebug do it?
Antony Carthy
Click Upvote
I thought that would be too memory intensive, and also beyond the scope of what I needed to achieve, so I simply ran a set of timeouts to handle the unprocessed images. Thanks for the help - have upvoted you.
Antony Carthy
A: 

Detecting when an ajax fetch is easy: use the Ajax Events defined in jQuery: http://docs.jquery.com/Ajax

But I'm not sure you are asking the same thing. To detect when an image finishes loading (presumably, this will be equal to an event when the image's src attribute is changed), you can use mutation events. http://wiki.github.com/jollytoad/jquery.mutation-events cheers,

jrh.

Here Be Wolves
The images src would always be set to the resource... when the resource is fetched it won't change.
Antony Carthy
+1  A: 

If you are calling $.ajax you can have a set a callback function for a successful request:

$.ajax({
    url: _url,
    type: _type,
    data: _data,
    dataType: _dataType,
    success: _success
})

var _success = function(result) {
    $html = $(result);
    alert('HTML downloaded');

    // inject HTML into DOM here
}

You'll need to go through $html to get the image elements and for each of those images register the load function callback which fires when an image had loaded:

$('#myImage').load(function() {  
    alert('Image Loaded');  
});
aleemb
The images won't be loaded immediately after the AJAX call.. the DOM will be updated and then the resources will be fetched.
Antony Carthy
Aah I see. I have updated the answer slightly which should get you on track.
aleemb
I will try use this function to match the image count to the loaded image count, thanks...
Antony Carthy