0

I have some code which does the following:

  1. External content is loaded via AJAX (video thumbnail images)
  2. The new content is then inserted into a div using $("#content").append();
  3. A mobile touch scrolling helper (iScroll) is applied to this div.

However the jQuery "load" event is not firing when the DOM changes due to an AJAX event, which means the call to initialise the scroller is happening too soon (before the images inthe content has loaded) which means it often doesn't get intiiallised. Without waiting for the images to load the content box is often short enough such that a scroll function isn't needed, but then when the images subsequently load, the box is not scrollable.

$("#videoList").append(videoThumbnails);

$(document).load(function () {
  // doesn't fire
  initScroller(); 
});

It appears that jQuery's append function does not block until all images referenced in the appended HTML have loaded.

How can I detect that all of the images loaded by the AJAX function have finished loading in order to call the initScroller() function AFTER all images have loaded?

5
  • The load event doesnt fire once content added using AJAX is loaded - it fires on initial load Commented Nov 12, 2012 at 15:32
  • 1
    possible duplicate of jquery: event for when ajax loaded content is all loaded (including images) Commented Nov 12, 2012 at 15:34
  • The load event does not propagate, and the document does not have a load event. Commented Nov 12, 2012 at 15:36
  • you can always use the ajax complete event, to trigger the events after ajax completes instead of using page load. More info: http://api.jquery.com/jQuery.ajax/ and http://docs.jquery.com/Ajax_Events Commented Nov 12, 2012 at 15:40
  • Thanks but that also has the same problem. It triggers only when the AJAX event completes, not when all dependent images have loaded. Commented Nov 12, 2012 at 15:51

2 Answers 2

3

OK I've found the solution in another similar question. It turns out there's a jQuery waitForImages plugin which does exactly what I want:

So I can just do this:

$("#videoList").waitForImages(function () {
    // Fires when all images in the #videoList div have loaded
    initScroller();
});
Sign up to request clarification or add additional context in comments.

Comments

2

The methods you are trying to use are triggered only once, when the page is loaded, but not for changes you make to the DOM aftewards (e.g. inserting content with ajax).

If you want to observer DOM changes you can use the DOMNodeInserted event

$(document).bind("DOMNodeInserted", function(event) {   ....do stuff...here     });

But generally it would be better to trigger this with the ajax callback.

$('#targetElementForYourContent').load('server/url.html', function() {
     ...do stuff here....
});

Comments

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.