Defer JS loading - insert to Head or Body

拥有回忆 提交于 2019-12-12 04:59:10

问题


I wanted to know what is the most recommended way in terms of optimization, caching and page load speed. Should I dynamically insert the JS files in the BODY or HEAD of my website document when using Defer loading?


回答1:


If you are using DEFER, it does not matter whether you put the script tag in the HEAD or BODY as far as when the script will run, or cache. Either way, it will run after the page is finished parsing, and will cache according to the server headers sent with the javascript file.

You may still want to put the script tags at the end of the body to optimize for the case where an older browser ignores the DEFER attribute.

Alternatively, you may want to put the script tags in the HEAD to get the javascript files earlier in the fetching order (before images, audio, or video files specified in the body, for example).



来源:https://stackoverflow.com/questions/11337112/defer-js-loading-insert-to-head-or-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!