Is it necessary to set onload function before setting src for an image object?

后端 未结 5 1550
别那么骄傲
别那么骄傲 2020-12-01 14:42

I was told it is necessary to set the onload function before setting src for an image object. I\'ve searched in SO for this.

I found this c

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 14:49

    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.

提交回复
热议问题