Kevin Powell - CommunityKP-C
Kevin Powell - Community4mo ago
24 replies
Faker

Fallback mechanism when img src is invalid

Hello, was surfing on stackoverflow forums when I came across this:

onerror="this.onerror=null;this.src='images/404-error.png';"

The onerror attribute, I tried it, seems really nice, when my img src is invalid, I got that custom image instead of that little icon which appears when image is not loaded.

I wanted to discuss a bit about what the onerror is and when it occurs, from what I've read, it occurs each time an internal resource (video/img) fails to load?

How does the this.onerror=null; work though? I read that we need it to prevent an infinite loop.
Was this page helpful?