JavaScript - overwriting .onload prototype of HTMLImageElement(s)

前端 未结 4 2299
花落未央
花落未央 2020-12-21 17:50

Is it possible to bind an onload event to each image, declaring it once? I tried, but can\'t manage to get it working... (this error is thrown: Uncaught

4条回答
  •  旧巷少年郎
    2020-12-21 18:42

    This provides a notification for any image loading, at least in Opera (Presto) and Firefox (haven't tried any other browser). The script tag is placed in the HEAD element so it is executed and the event listener installed before any of the body content is loaded.

    document.addEventListener('load', function(e) {
        if ((!e.target.tagName) || (e.target.tagName.toLowerCase() != 'img')) return;
        // do stuff here
    }, true);
    

    Of course, by changing the filtering on tagName it will also serve to respond to the loading of any other element that fires a load event, such as a script tag.

提交回复
热议问题