Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect failed img nodes, server controlled img.alt #93

Open
sanjarcode opened this issue Aug 29, 2023 · 1 comment
Open

Detect failed img nodes, server controlled img.alt #93

sanjarcode opened this issue Aug 29, 2023 · 1 comment
Assignees

Comments

@sanjarcode
Copy link
Member

sanjarcode commented Aug 29, 2023

  • works for traditional as well as SPA.
  • works if the image is loading, or has loaded.
function actionWhenImageDidntLoad() {
  console.log("Image couldnt load");
}

document.querySelectorAll("img").forEach((imgNode) => {
  const isFetching = !imgNode.complete;
  if (isFetching) {
    imgNode.addEventListener("error", actionWhenImageDidntLoad);
    return;
  }

  const couldntLoadImage = !imgNode.naturalWidth;
  if (couldntLoadImage) {
    actionWhenImageDidntLoad();
    return;
  }
});

context: i was working on a traditional web app, where JS runs after page DOM has settled. the goal was to makeimg.alt server controlled (assume I own the image server, which would sent the alt with an error message).

@sanjarcode sanjarcode self-assigned this Aug 29, 2023
@sanjarcode
Copy link
Member Author

sanjarcode commented Aug 29, 2023

problem: can't access asset call error. where is it stored btw. Maybe it is accessible in the error event case. what about the already loaded ones.

if devTools have it, it may be stored by the page too.

@sanjarcode sanjarcode changed the title Detect failed img nodes Detect failed img nodes, server controlled img.alt Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant