JavaScript标签<script>中的defer和async


<script>

Let’s start by defining what <script> without any attributes does. The HTML file will be parsed until the script file is hit, at that point parsing will stop and a request will be made to fetch the file (if it’s external). The script will then be executed before parsing is resumed.

<script async>

async downloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.

<script defer>

defer downloads the file during HTML parsing and will only execute it after the parser has completed. defer scripts are also guarenteed to execute in the order that they appear in the document.

When should I use what?

Typically you want to use async where possible, then defer then no attribute. Here are some general rules to follow:

  • If the script is modular and does not rely on any scripts then use async.
  • If the script relies upon or is relied upon by another script then usedefer.
  • If the script is small and is relied upon by an async script then use an inline script with no attributes placed above the async scripts.

Thanks:
http://peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/
http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值