Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script标签async和defer #33

Open
YBFACC opened this issue Jul 22, 2020 · 0 comments
Open

Script标签async和defer #33

YBFACC opened this issue Jul 22, 2020 · 0 comments

Comments

@YBFACC
Copy link
Owner

YBFACC commented Jul 22, 2020

Script标签async和defer

async :可选。表示应该立即下载脚本,但不应妨碍页面中的其他操作,比如下载其他资源或等待加载其他脚本。只对外部脚本文件有效。
defer :可选。表示脚本可以延迟到文档完全被解析和显示之后再执行。只对外部脚本文件有效。IE7及更早版本对嵌入脚本也支持这个属性。

引用来自《JavaScript高级程序设计(第3版) 》

原图链接https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

bVWhRl

放在head中不使用async和defer

会等待所有资源下载完毕,并按照脚本的顺序进行执行

不使用

放在body底部

常见的方式

放在body底部

放在head中使用async

会异步下载脚本,不会影响页面的解析。并且执行时,不会按照脚本的顺序而是谁先下载完谁先执行。

async

放在head中使用defer

异步下载脚本,在文档渲染完之后,在 DOMContentLoaded时间触发前,按照脚本的顺序进行执行

defer

放在head中同时使用async和defer

表现与只使用async相同。

同时使用

参考

defer和async的区别

浅谈script标签的defer和async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant