JavaScript标签<script>中的defer和async
2015-02-06 16:54
666 查看
<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>
asyncdownloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.
<script defer>
deferdownloads the file during HTML parsing and will only execute it after the parser has completed.
deferscripts are also guarenteed to execute in the order
that they appear in the document.
When should I use what?
Typically you want to use asyncwhere possible, then
deferthen 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 use
defer.
If the script is small and is relied upon by an
asyncscript then use an inline
scriptwith no attributes placed above the
asyncscripts.
Thanks:
http://peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/ http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html
相关文章推荐
- HTML5 <script>元素async,defer异步加载
- HTML5 <script>元素async,defer异步加载
- HTML5 <script>元素async,defer异步加载
- HTML5 <script>元素async,defer异步加载
- HTML5 <script>元素async,defer异步加载
- HTML5 <script>元素async,defer异步加载
- javascript:&lt;/script&gt;标签不允许出现在注释或字符串中,否则解释出错
- HTML5 <script>元素async,defer异步加载
- <script>, <script async>, <script defer> 三种标签的区别
- <script type="text/javascript" defer="defer">
- 超链接<a>标签和javascript:void(0)
- <script type="text/javascript">
- js <script> async 属性 异步
- 超链接<a>标签和javascript:void(0)
- <script>标签的加载解析执行
- HTML 5 <script> defer 属性
- <script language= "javascript " for= "window " event= "onload " /> 的问题解读
- <script>标签书写位置
- <a>标签中href="javascript:;"
- js:在<script></script>中使用的属性,可以使标签对里的内容当成文本