Javascript non-blocking scripts, why don't simply put all scripts before </body> tag?

前端 未结 3 1932
渐次进展
渐次进展 2020-12-31 09:04

In order to avoid javascript to block webpage rendering, can\'t we just put all all our JS files/code to be loaded/executed simply before the closing

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 10:08

    Generally saying no. Even if scripts will be loaded after all the content of the page, loading and executing of the scripts will block the page. The reason for that is possibility of presence of write commands in your scripts.

    However if all you want to achieve is the speed of loading page contents, the result of placing script tags right before tag is the same as for creating script tags dynamically. The most significant difference is that when you load scripts in common static way they are executed one by one, in other words no parallel execution of script file (in old browsers the same true is for downloading of the script too).

提交回复
热议问题