I'm trying to perform an Ajax call with jQuery which is working well. I use the success event to display the data. It seems however that success gets triggered as soon as the external HTML file gets loaded. If there are large images they keep loading after they're shown. Is there a way to display the content after everything is fully loaded? Here is the code:
$('#divajax').html('<br><div style="text-align: center;"><img src="res/ajax-loader.gif"></div>');
$.ajax({
cache: false,
url: 'ajax/content.php',
success: function(data) {
$('#divajax').html(data);
}
});
this triggers on every img load separately:
i used:
Try this code. It works fine.
Look into using jquery's .load()
Which has a description of :
The plugin that @alex wrote didn't work for me for some reason... I couldn't figure out why. But his code did inspire me to come up with a more lightweight solution that is working for me. It uses jquery promises. Please note that unlike @alex 's plugin, this doesn't attempt to account for background images on elements, only img elements.
Then you can use it something like this:
Hopefully that's helpful for someone.
Note that using the above code, if one of the images errors (eg because the URL was wrong), the promise is resolved anyway and the error is ignored. This might be what you want, but, depending on your situation, you might instead want to abort whatever you are doing if an image fails to load. In which case you could replace the onerror line as follows:
And catch the error like this:
You can bind something to the load events to know when they're done:
Or you could use this plugin.