views:

489

answers:

3

Hi,

I'm loading html from an ajax request, and some img tags in it.

I need to know when the images loaded in the ajax are fully loaded to resize the container. I don't know how many images are in the result, so I can't simply check the .complete value.

Do you know a solution for this?

Thank you!

Leandro

+1  A: 

If you're using jQuery, the $(window).load() command can set up a function to be called once all the images are loaded, as follows:

$(window).load(
    function() {
        // weave your magic here.
    }
);

If you're not using jQuery, I'd just do what I've done when I want a little bit of its functionality: download it and examine the source to see how it does it. Then do that :-)

paxdiablo
Well, the ajax loads the images when the user requests it. So it's a lot after that the content load.Is possible to use the .load() function in any element? maybe I could hook that event to the container.
Leandro Ardissone
A: 

XMLHTTPrequests have properties that you can use to determine when the load has completed. If you are using javascript directly, you would need to assign a function to the onreadystatechange event. This is called each time that the state changes. The states are stored in ReadyState and are: 0 Uninitiated 1 Loading 2 Loaded 3 Interactive 4 Complete

In your function you check that the status is 4 and call the other required functions

JQuery has events that tell you when certain things have occurred. View the AJAX Documentation

Sean
Yes, but there's no event for when the images of the resultant ajax request are loaded.The .load() is the solution.Thanks anyway.
Leandro Ardissone
A: 

Ok, thanks to Pax, I've found the solution.

On the success event of the ajax request (using jQuery), I've added the following code:


$('img', this).load(function() {
    resize_element(this);
});

Thanks both of you!

Leandro Ardissone