How to make JavaScript execute after page load?

前端 未结 24 2768
孤城傲影
孤城傲影 2020-11-21 05:55

I\'m executing an external script, using a

24条回答
  •  生来不讨喜
    2020-11-21 06:27

    If the scripts are loaded within the of the document, then it's possible use the defer attribute in script tag.

    Example:

    
    

    From https://developer.mozilla.org:

    defer

    This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded.

    This attribute must not be used if the src attribute is absent (i.e. for inline scripts), in this case it would have no effect.

    To achieve a similar effect for dynamically inserted scripts use async=false instead. Scripts with the defer attribute will execute in the order in which they appear in the document.

提交回复
热议问题