jQuery callback on image load (even when the image is cached)

前端 未结 14 1177
轻奢々
轻奢々 2020-11-21 06:25

I want to do:

$(\"img\").bind(\'load\', function() {
  // do stuff
});

But the load event doesn\'t fire when the image is loaded from cache

14条回答
  •  鱼传尺愫
    2020-11-21 07:26

    A solution I found https://bugs.chromium.org/p/chromium/issues/detail?id=7731#c12 (This code taken directly from the comment)

    var photo = document.getElementById('image_id');
    var img = new Image();
    img.addEventListener('load', myFunction, false);
    img.src = 'http://newimgsource.jpg';
    photo.src = img.src;
    

提交回复
热议问题