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

后端 未结 2 1783
伪装坚强ぢ
伪装坚强ぢ 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:25

    Is 'el' already part of your live DOM? If so, when you change its onload event handler, it's contents will not be evaluated (because the load event already occurred).

    el.onload = function() { //...

    If el is not already added to the page, for instance if you're building a view, and will be injected into the page when everything is set, then yes it's load event will be fired when it's added to the page.

    Be cautious about what might in the 'src' might depend on what happened in 'onload'.

提交回复
热议问题