Are deferred scripts executed before DOMContentLoaded event?

前端 未结 3 871
旧巷少年郎
旧巷少年郎 2020-12-13 07:20

Upon defer attirbute MDN says:

This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has

3条回答
  •  春和景丽
    2020-12-13 07:35

    DOMContentLoaded can be fired before CSSOM, source

    The domContentLoaded event fires shortly after the HTML is parsed; the browser knows not to block on JavaScript and since there are no other parser blocking scripts the CSSOM construction can also proceed in parallel.

    Article on Google Developer describes async instead of defer but in the case of your question, it doesn't change anything because based on Steve Sourders article on perfplanet

    DEFER scripts execute after DOM Interactive.

    and his comment under his article

    [...] the spec says that DEFER scripts run after domInteractive but before domContentLoaded.

    You can make your own experiment, look below for code using defer and timeline

    
    
    
      
      JS Bin
      
      
    
    
      

    App

提交回复
热议问题