Skip to content

Instantly share code, notes, and snippets.

@kozo002
Created March 15, 2012 03:32
Show Gist options
  • Select an option

  • Save kozo002/2041709 to your computer and use it in GitHub Desktop.

Select an option

Save kozo002/2041709 to your computer and use it in GitHub Desktop.
画像のonloadイベントを扱う際のjQuery拡張
(function($) {
var imageLoad = function(callback) {
var src, timestamp;
src = this.attr('src');
timestamp = new Date().getTime();
this.attr({src: src + '?' + timestamp});
return this.load(callback);
};
var createImageAndLoad = function(image_path, callback) {
var createImage = function(image_path) {
return $('<img />').attr({src: image_path});
};
if (typeof image_path === 'string') {
createImage(image_path).imageLoad(callback);
} else if (typeof image_path === 'object') {
$.each(image_path, function(i, imgp) {
createImage(imgp).imageLoad(callback);
});
}
};
$.fn.extend({
imageLoad: imageLoad
});
$.extend({
imageLoad: createImageAndLoad
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment