Skip to content

Instantly share code, notes, and snippets.

@venkatd
Created September 17, 2011 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venkatd/1224290 to your computer and use it in GitHub Desktop.
Save venkatd/1224290 to your computer and use it in GitHub Desktop.
jQuery image loaded special event
(function ($) {
function img_has_loaded(imgEl) {
var dfd = $.Deferred();
var img = new Image();
img.onload = function() {
if (!dfd.isResolved())
dfd.resolve();
}
img.src = $(imgEl).attr('src');
if (img.complete && !dfd.isResolved()) {
dfd.resolve();
}
return dfd.promise();
}
$.event.special.imageload = {
add: function(details) {
var self = $(this);
var images = $(this).is('img') ? $(this) : $(this).find('img');
var dfds = [];
images.each(function() {
dfds.push( img_has_loaded( this ) );
});
$.when.apply(this, dfds).then(function() {
details.handler.call(this, {type: 'imageload'});
});
},
remove: function(details) {
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment