You need to get rid of the timeout delay, since it is not guaranteed long enough to download images (slow server, slow Internet, etc.).
Unfortunately, the βrightβ solution you were looking for is not the easiest because you can only access the img nodes, which after requesting $("#ajaxoutput").html(data) put them in their place . In the next statement, it will be unclear (due to browser behavior) whether the images will already be downloaded (from the cache) or not. Therefore, simply connecting the onload handler would be unreliable.
The workaround is to create a bunch of off-screen img nodes that reflect those in "#ajaxoutput" and promise them in such a way that their src properties are set after attaching onload (and onerror).
The code is not easy to follow, so I did my best to give explanatory comments.
function display_result(data) {
Without comment you will see that it is actually very concise and hopefully a little less scary.
function display_result(data) { function imgPromise(index, imgNode) { return $.Deferred(function(dfrd) { $("<img/>").on('load error', dfrd.resolve).attr('src', imgNode.src); }).promise(); } var promises = $("#ajaxoutput").html(data).find("img").map(imgPromise); return $.when.apply(null, promises); }
Call the following:
getData(table).then(display_result).then(equalise);
source share