It doesn’t have to, but if setting the src
and the image loads before your handler is attached, it won’t fire.
JavaScript operates asynchronously. Setting the src
will cause the web browser to load the image outside the main execution flow. If onload
isn’t set at the time that operation completes – which could be between setting src
and onload
.