Why put JavaScript in head

后端 未结 8 1926
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 11:18

If it is perfectly acceptable to put JavaScript right before what is a good reason to keep it in the ?

Based on t

8条回答
  •  孤城傲影
    2020-12-24 11:27

    The reason behind this is as the Head gets loaded before the body. Any dynamic javascript code that gets executed in the body on load will execute correctly.

    If you have javascript that is just before the tag then any javascript calls made to functions by your page as it loads will error.

    So yes putting javascript before the tag will load faster. But only if your javascript will be executed after page load via clicks for example.

提交回复
热议问题