I see two different solutions on this issue and it depends on your goal.
In the code you use $(document).ready(); that starts when the DOM is ready, and not when the assets are fully loaded, it makes sense to detect when the image is loaded, but in this case I would go to use my own Image () object and avoid using .load() , since I found it extremely unreliable in a few cases, and as far as I know, sometimes it doesnβt work fine if the images are downloaded from the browser cache.
$('#gallery img').each(function() { var currentimage = new Image(); currentimage.src = this.src; currentimage.onload = function(){ numLoaded += 1; alert("Loaded: " + numLoaded); if (numLoaded == numToLoad) { var gallery = $('#gallery').unslider(), data = gallery.data('unslider'); data.to(0); } }; });
Another solution (which I prefer to use here) is to use window.onload , which starts when your document and assets are fully loaded, and you should use it this way:
window.onload = function(){
Mack
source share