Skip to content

Avoid firing 'load' or 'error' events for <img> when the loaded image didn't change #8492

Open

Description

@zcorpan

Currently, in the "update the image data" algorithm for img, if the loaded image is the same as the previous image, it will still fire a load event. Similarly for error (see #4480).

Investigate if it's compatible to change this to avoid firing load or error when there was no change (other than updating the current pixel density).

See discussion in #8008

@zcorpan

If this change appears too risky, I think we could first try to change the “load the image data” algorithm to avoid firing “load” and “error” events in more cases. If that is compatible, then it should alleviate the main compat risk for auto-sizes.

@yoavweiss

At the same time, I kinda like the option of avoiding firing the "load"/"error" events in cases where the image didn't actually loaded.

Activity

  1. added
    topic: img
    topic: events
    on Nov 10, 2022
  2. shannonbooth commented on May 26, 2026

    @shannonbooth
    Member

    One question I have stumbled across on the same topic - should adopting an already-loaded image into a new document should fire a load event?

    Chromium and Webkit seem to say no to this - Firefox indicate that it should. Update the image data says "Wait until the element's node document is fully active" which can be interpreted as meaning either the node document at the time that step is reached or when an active document is adopted.

    Chromium and Webkit dont fire a load event for below, Firefox does. I think I would expect a load event to be fired.

    Test below:

    <!doctype html>
    <meta charset="utf-8">
    <title>Image load after adoption from removed iframe</title>
    <pre id="log"></pre>
    <script>
    function log(message) {
        document.getElementById("log").textContent += `${message}\n`;
    }
    
    const iframe = document.createElement("iframe");
    
    iframe.onload = () => {
        const img = iframe.contentDocument.createElement("img");
    
        img.onload = () => {
            log("image loaded after adoption");
            log(`width=${img.width} height=${img.height} naturalWidth=${img.naturalWidth} naturalHeight=${img.naturalHeight} complete=${img.complete}`);
        };
    
        img.onerror = () => {
            log("image errored after adoption");
        };
    
        img.src = "120.png";
        iframe.contentDocument.body.appendChild(img);
    
        iframe.remove();
        document.body.appendChild(document.adoptNode(img));
    };
    
    document.body.appendChild(iframe);
    </script>

    One other test case is:

    <!doctype html>
    <meta charset="utf-8">
    <title>Already-loaded image adoption from iframe</title>
    <pre id="log"></pre>
    <script>
    function log(message) {
        document.getElementById("log").textContent += `${message}\n`;
    }
    
    const iframe = document.createElement("iframe");
    iframe.src = "blank.html";
    
    iframe.onload = () => {
        const img = iframe.contentDocument.createElement("img");
    
        img.onload = () => {
            // Image is now completely available in the iframe's document.
            // Reset the handler and adopt into the main document.
            let fired = false;
            img.onload = () => { fired = true; };
            img.onerror = () => { fired = true; };
    
            iframe.remove();
            document.body.appendChild(document.adoptNode(img));
    
            setTimeout(() => {
                log(fired
                    ? "load fired after adoption of already-loaded image"
                    : "load did not fire after adoption of already-loaded image");
            }, 500);
        };
    
        img.onerror = () => {
            log("image errored during initial load");
        };
    
        img.src = "120.png";
        iframe.contentDocument.body.appendChild(img);
    };
    
    document.body.appendChild(iframe);
    </script>
  3. jdm commented on Aug 4, 2026

    @jdm
    Member

    I'm confused—the following testcase only reports a single load event across Firefox, Safari, and Chrome:

    <script>
    let e = document.createElement('img');
    e.src = "https://joshmatthews.net/portrait.jpg";
    e.onload = () => {
      console.log('onload');
      e.width *= 2;
    };
    </script>

    This doesn't appear to match what the specification describes or what this issue describes.

  4. evlasics commented on Aug 4, 2026

    @evlasics

    @jdm I think that this might actually be the behavior we expect based on what this issue describes? Since setting e.width *= 2 doesn't update the image (maybe with srcset and lazy loading it could be different), we should expect a single onload event from when the image loads initially, then none from modifying the width.

    That said, if we replace e.width *= 2 with e.sizes = "auto" in the example, then chromium and safari send an endless stream of load events, whereas firefox sends two. In firefox it seems to only trigger when e.sizes is changed, rather than set, so one accounts for the load of the image, and the other is the initial set of e.sizes—I think this is the worst of both worlds.

    Replacing e.width *= 2 with e.src = "https://joshmatthews.net/portrait.jpg" gives endless streams of onload events in all three browsers, which is what we should expect from all these test cases according to the current spec, but not from this issue.

    In any case, the spec probably needs to be updated here to clarify what the intended behavior is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs compat analysistopic: eventstopic: img

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions