31

This question has been asked a couple times but not answered in such a way that it can help me with my specific issue. From a nav list, on click of an item, I'm loading some HTML content into a div using the .html() function. There is no ajax request. The HTML content has images. Hence it can take a moment to load up. And since .html() is a synchronous operation, the next line will immediately execute.

As soon as I load contents using .html(), I'm enabling a third party custom scrollbar called tinyscrollbar. If the loaded content had images, then the scrollbar calculates the height of the content div a little earlier than the images are loaded resulting into a scrollbar that won't scroll all the way.

I do not want to use .setInterval(). Is there a solution for this? Is there some other function in jQuery that acts like the .html() function but has some sort of callback feature?

Thank you.

7
  • 3
    You could determine how many images were in the content, then listen for their onload events Commented Apr 24, 2012 at 15:12
  • Did you try defining a set width and height on each image tag? Even if the images are not loaded yet, the scrollbar should be able to calculate the height correctly if it knows the image height. Commented Apr 24, 2012 at 15:15
  • Would $(document).ready not work since its waiting on images? Commented Apr 24, 2012 at 15:15
  • robinson comment seems the right answer. You can simply iterate over the image with something like $("#mydiv img"). Commented Apr 24, 2012 at 15:20
  • @MatthewRiches $(document).ready would not work because it does not wait for images and css to be done loading. Commented Apr 24, 2012 at 15:20

4 Answers 4

60
$('#divId').html(someText).promise().done(function(){
    //your callback logic / code here
});
Sign up to request clarification or add additional context in comments.

5 Comments

Be careful linking to (I assume) your blog excessively with little other information, such as your similar answer here. It can be seen as spam.
It's better to use then(), instead of done() to be more compliant with standard JavaScript Promise object: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
This answer is misleading. .html() is synchronous. .promise().done() isn't waiting for the .html() method to complete. Instead, it's simply running the callback on the next tick of the event loop, similar to what would happen if you used setTimeout(yourLogicFunction, 0). It may "work", but it's not waiting for .html() to complete.
@Deb is this call documented on the official API docs , because i have have been looking around this as i have concerns around browser compatibility .
Here is the link, that is in concern in the comments, found it good to be pasted.. and plus, the solution definitely works!! https://digitizor.com/jquery-html-callback-function-using-promise/
12

Based on Mike Robinson's (and dystroy's) suggestion the answer to my question is:

$("#myContentDiv").html('HTML content comes here');
var contentImages = $("#myContentDiv img");
    var totalImages = contentImages.length;
    var loadedImages = 0;
    contentImages.each(function(){
        $(this).on('load', function(){
            loadedImages++;
            if(loadedImages == totalImages)
            {
                $("#myContentDiv").tinyscrollbar();
            }
        });
    });

3 Comments

I had a similar problem with an iScroll and (not properly loaded) images too. Due to not loaded images the parent DIV was not high enough and the scroll did not show whole content. This solution works!
This is the correct answer. If there are images added with .html() they need to load first.
Yes - THIS is the correct answer. Don't know why the answer above is marked correct.
-4

In order to check whether image loaded or not you can use following;

<img src="your_image_path" />


<script>
    $('img').load(function() {
        //call_your_function();
    });
</script>

Comments

-6
$(window).load(function() {
  // code here
})();

1 Comment

Question is about callback of HTML , not about windows load event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.