Is the order of onload handler and src set important in a script element?

后端 未结 2 1780
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 03:45

Coming from this answer that says:

You should set the src attribute after the onload event, f.ex:



        
2条回答
  •  灰色年华
    2021-02-19 04:24

    @thinkbigthinksmall is right.

    I would add that this behavior is webkit specific, Firefox will queue the event handler, so in this case it wouldn't matter.

    I am not sure what the specs do say about it, but IMM FF behavior is the correct one.

    Anyway, since webkit behaves like that, one should always set the src after the onload event has been declared, otherwise, cached media will fire the onload event before your script has been parsed.

    One workaround though is to set your src again after the onload event has been declared :

    
    
    

    This way, you're sure the onload event will fire. Also, a single request should be made, since the first one will be cached.

提交回复
热议问题