最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the difference between deferred scripts and scripts placed at the end of the page body? - Stack Over

programmeradmin4浏览0评论

HTML5 introduced the defer attribute for scripts whose loading can be deferred in a HTML page. defer may be used for any scripts that don't need to be loaded before the DOM (a.k.a don't mess with the DOM before it is ready).

For a long time web developers have been advised to put all scripts that don't need to be loaded before the DOM not in the page head but before the end of the body tag instead.

What is the difference between the use of defer and the long practiced advise? Does the first substitute the latter?

I appreciate any answer. Thank you.

HTML5 introduced the defer attribute for scripts whose loading can be deferred in a HTML page. defer may be used for any scripts that don't need to be loaded before the DOM (a.k.a don't mess with the DOM before it is ready).

For a long time web developers have been advised to put all scripts that don't need to be loaded before the DOM not in the page head but before the end of the body tag instead.

What is the difference between the use of defer and the long practiced advise? Does the first substitute the latter?

I appreciate any answer. Thank you.

Share Improve this question asked Nov 1, 2012 at 18:04 rodrigoalvesvieirarodrigoalvesvieira 8,06215 gold badges65 silver badges85 bronze badges 2
  • 1 I believe script is still going to be downloaded even if you use defer, and it may slow done overall page load – Evgeny Commented Nov 1, 2012 at 18:08
  • 4 The idea behind putting script at the bottom is not only to delay execution, but also to delay download – Evgeny Commented Nov 1, 2012 at 18:09
Add a comment  | 

1 Answer 1

Reset to default 18

Both async and defer scripts begin to download immediately without pausing the parser and both support an optional onload handler to address the common need to perform initialization which depends on the script.

From the WebKit blog, so the behaviour is not necessarily the same across all browsers. So, performance would be better if the scripts are still at the end, as they will be downloaded later.

Edit 2017: browser support is now much better, so you can probably get away with async/defer scripts in the head. It's still probably a safer choice to put them at the bottom; new browsers will still download them early even if they're not in the head.

Edit 2020: These days, unless you're supporting very old browsers, you should just go ahead and use async/defer in the head.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论