本文翻译自:Script Tag - async & defer

I have a couple of questions about the attributes async & defer for the <script> tag which to my understanding only work in HTML5 browsers. 我有几个关于<script>标签的asyncdefer属性的问题,据我所知,只能在HTML5浏览器中使用。

One of my sites has two external JavaScript files that currently sit just above the </body> tag; 我的一个网站有两个外部JavaScript文件,目前位于</body>标记的上方; the first is jquery sourced from google and the second is a local external script. 第一个是来自谷歌的jquery ,第二个是本地外部脚本。

With respects to site load speed 关于网站加载速度

  1. Is there any advantage in adding async to the two scripts I have at the bottom of the page? async添加到页面底部的两个脚本中是否有任何优势?

  2. Would there be any advantage in adding the async option to the two scripts and putting them at the top of the page in the <head> ? async选项添加到两个脚本并将它们放在<head>页面的顶部是否有任何优势?

  3. Would this mean they download as the page loads? 这是否意味着他们下载页面加载?
  4. I assume this would cause delays for HTML4 browsers, but would it speed up page load for HTML5 browsers? 我认为这会导致HTML4浏览器的延迟,但是它会加速HTML5浏览器的页面加载吗?

Using <script defer src=... 使用<script defer src=...

  1. Would loading the two scripts inside <head> with the attribute defer the same affect as having the scripts before </body> ? 使用属性加载<head>的两个脚本是否会defer</body>之前的脚本相同的影响?
  2. Once again I assume this would slow up HTML4 browsers. 我再次假设这会降低HTML4浏览器的速度。

Using <script async src=... 使用<script async src=...

If I have two scripts with async enabled 如果我有两个启用了async脚本

  1. Would they download at the same time? 他们会同时下载吗?
  2. Or one at a time with the rest of the page? 或者与页面的其余部分一次一个?
  3. Does the order of scripts then become a problem? 脚本的顺序是否会成为问题? For example one script depends on the other so if one downloads faster, the second one might not execute correctly etc. 例如,一个脚本依赖于另一个脚本,因此如果下载速度更快,则第二个脚本可能无法正确执行等。

Finally am I best to leave things as they are until HTML5 is more commonly used? 最后,我最好保留原样,直到HTML5更常用?


#1楼

参考:https://stackoom.com/question/jLgL/脚本标记-异步和延迟


#2楼

Keep your scripts right before </body> . </body>之前保持脚本正确。 Async can be used with scripts located there in a few circumstances (see discussion below). 在某些情况下,Async可以与位于那里的脚本一起使用(参见下面的讨论)。 Defer won't make much of a difference for scripts located there because the DOM parsing work has pretty much already been done anyway. Defer不会对位于那里的脚本产生很大的影响,因为DOM解析工作已经完成了很多工作。

Here's an article that explains the difference between async and defer: http://peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/ . 这篇文章解释了异步和延迟之间的区别: http : //peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/ 。

Your HTML will display quicker in older browsers if you keep the scripts at the end of the body right before </body> . 如果您在</body>之前将脚本保留在正文末尾,那么您的HTML将在旧版浏览器中更快地显示。 So, to preserve the load speed in older browsers, you don't want to put them anywhere else. 因此,为了保持旧版浏览器的加载速度,您不希望将它们放在其他任何位置。

If your second script depends upon the first script (eg your second script uses the jQuery loaded in the first script), then you can't make them async without additional code to control execution order, but you can make them defer because defer scripts will still be executed in order, just not until after the document has been parsed. 如果您的第二个脚本依赖于第一个脚本(例如,您的第二个脚本使用第一个脚本中加载的jQuery),那么您无法在没有其他代码来控制执行顺序的情况下使它们异步,但是您可以使它们延迟,因为延迟脚本将仍然按顺序执行,直到文档被解析后才执行。 If you have that code and you don't need the scripts to run right away, you can make them async or defer. 如果您拥有该代码并且不需要立即运行脚本,则可以使它们异步或延迟。

You could put the scripts in the <head> tag and set them to defer and the loading of the scripts will be deferred until the DOM has been parsed and that will get fast page display in new browsers that support defer, but it won't help you at all in older browsers and it isn't really any faster than just putting the scripts right before </body> which works in all browsers. 您可以将脚本放在<head>标记中并将它们设置为defer并且脚本的加载将推迟到解析DOM之后,并且将在支持延迟的新浏览器中显示快速页面,但它不会在旧版浏览器中对你提供帮助,并不比在所有浏览器中都能正常使用</body>之前的脚本快得多。 So, you can see why it's just best to put them right before </body> . 所以,你可以看到为什么最好把它们放在</body>之前。

Async is more useful when you really don't care when the script loads and nothing else that is user dependent depends upon that script loading. 当您在脚本加载时并不关心时,Async会更有用,并且用户所依赖的任何其他内容都不依赖于该脚本加载。 The most often cited example for using async is an analytics script like Google Analytics that you don't want anything to wait for and it's not urgent to run soon and it stands alone so nothing else depends upon it. 最常被引用的使用异步的示例是像Google Analytics这样的分析脚本,您不需要等待任何事情,并且不急于即将运行并且它是独立的,因此没有其他任何依赖它。

Usually the jQuery library is not a good candidate for async because other scripts depend upon it and you want to install event handlers so your page can start responding to user events and you may need to run some jQuery-based initialization code to establish the initial state of the page. 通常jQuery库不适合async,因为其他脚本依赖它并且你想安装事件处理程序,所以你的页面可以开始响应用户事件,你可能需要运行一些基于jQuery的初始化代码来建立初始状态的页面。 It can be used async, but other scripts will have to be coded to not execute until jQuery is loaded. 它可以用作异步,但是其他脚本必须编码才能在加载jQuery之前执行。


#3楼

HTML5: async , defer HTML5: asyncdefer

In HTML5, you can tell browser when to run your JavaScript code. 在HTML5中,您可以告诉浏览器何时运行JavaScript代码。 There are 3 possibilities: 有三种可能性:

<script       src="myscript.js"></script><script async src="myscript.js"></script><script defer src="myscript.js"></script>
  1. Without async or defer , browser will run your script immediately, before rendering the elements that's below your script tag. 如果没有asyncdefer ,浏览器将立即运行您的脚本,然后再渲染脚本标记下方的元素。

  2. With async (asynchronous), browser will continue to load the HTML page and render it while the browser load and execute the script at the same time. 使用async (异步),浏览器将继续加载HTML页面并在浏览器加载时呈现它并同时执行脚本。

  3. With defer , browser will run your script when the page finished parsing. 使用defer ,浏览器将在页面完成解析后运行您的脚本。 (not necessary finishing downloading all image files. This is good.) (没必要完成下载所有图像文件。这很好。)


#4楼

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. asyncdefer脚本都会立即开始下载而不会暂停解析器,并且都支持可选的onload处理程序,以满足执行初始化的常见需求,这取决于脚本。

The difference between async and defer centers around when the script is executed. asyncdefer之间的区别在于脚本执行时的中心。 Each async script executes at the first opportunity after it is finished downloading and before the window's load event. 每个async脚本在完成下载后和窗口加载事件之前的第一个机会执行。 This means it's possible (and likely) that async scripts are not executed in the order in which they occur in the page. 这意味着async脚本可能(并且可能)不按页面中出现的顺序执行。 Whereas the defer scripts, on the other hand, are guaranteed to be executed in the order they occur in the page. 而另一方面, defer脚本保证按照它们在页面中出现的顺序执行。 That execution starts after parsing is completely finished, but before the document's DOMContentLoaded event. 解析完成后,但在文档的DOMContentLoaded事件之前,执行开始。

Source & further details: here . 来源及进一步细节: 这里 。


#5楼

async and defer will download the file during HTML parsing. asyncdefer将在HTML解析期间下载文件。 Both will not interrupt the parser. 两者都不会中断解析器。

  • The script with async attribute will be executed once it is downloaded. 具有async属性的脚本将在下载后执行。 While the script with defer attribute will be executed after completing the DOM parsing. 具有defer属性的脚本将在完成DOM解析后执行。

  • The scripts loaded with async does n't guarantee any order. 使用async加载的脚本不保证任何订单。 While the scripts loaded with defer attribute maintains the order in which they appear on the DOM. 加载defer属性的脚本维护它们在DOM上的显示顺序。

Use <script async> when the script does not rely on anything. 当脚本不依赖任何内容时,请使用<script async> when the script depends use . 当脚本依赖使用时。

Best solution would be add the at the bottom of the body.There will be no issue with blocking or rendering. 最好的解决方案是添加在body的底部。阻塞或渲染没有问题。

UPDATE UPDATE

The answer has been corrected based on comments. 答案已根据评论予以纠正。


#6楼

This image explains normal script tag, async and defer 此图像解释了正常的脚本标记,异步和延迟

  • Async scripts are executed as soon as the script is loaded, so it doesn't guarantee the order of execution (a script you included at the end may execute before the first script file ) 加载脚本后立即执行异步脚本,因此不保证执行顺序(最后包含的脚本可能在第一个脚本文件之前执行)

  • Defer scripts guarantees the order of execution in which they appear in the page. 延迟脚本保证它们在页面中出现的执行顺序。

Ref this link : http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html 参考此链接: http : //www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

脚本标记 - 异步和延迟相关推荐

  1. android 开启一个定时线程_Android异步、延迟和定时任务的简易用法

    异步多线程 延迟触发 循环定时触发 一.前言 项目开发中一定会用到网络请求,文件读写,开启子线程进行耗时操作,延迟返回或关闭提示框,轮询接口获取处理结果,子线程发送内容到主线程更新界面等等.碰到这些问 ...

  2. MySQL异步复制延迟解决的架构设计与运维架构ppt

    <MySQL异步复制延迟解决的架构设计与运维架构ppt> 下载地址:网盘下载 转载于:https://www.cnblogs.com/long12365/p/9731216.html

  3. 何时在脚本标记中需要CDATA节?

    脚本标记中是否曾经需要CDATA标记?如果需要,何时? 换句话说,何时何地: <script type="text/javascript"> //<![CDATA ...

  4. html怎么用脚本显示隐藏,使用隐藏状态而不是注释或自定义脚本标记来模板化HTML...

    Javascript最佳做法&惯例,例如John Resig和Nicholas Zachas强调的那些,可维护JavaScript一书的作者,建议使用HTML注释或带有自定义类型的脚本标签来存 ...

  5. 前端学习(493):script之延迟脚本和异步脚本

  6. 每天学一点儿shell:shell脚本的异步执行

    文章目录 shell管道"|" shell并行执行"&" shell串行执行"&&" shell管道"|& ...

  7. linux 脚本的异步执行,shell命令同步异步

    问题:在 shell 下有 A B C D 四个命令,需要先执行 A 再执行 B C 最后执行 D 其中, B C 耗时较多, 但是,互不干扰,可以同步执 子进程 和 wait ./A ./B &am ...

  8. 用手动创建新的script标签的方式,实现JavaScript脚本的异步加载

    代码第四行里新建一个script标签页,在第8行的回调函数onreadystatechange里,根据属性readyState判断当前标签页的状态,如果为loaded或者complete,说明脚本加载 ...

  9. Linux系统中,shell脚本的异步执行

    在编写shell时候,大部分时候串行执行即可,个别场景下要让多个shell并行执行: 下面说明一下shell执行的三种执行顺序: 1.shell管道"|" 使用过命令行Linux系 ...

最新文章

  1. 2022-2028年中国再生天然橡胶行业市场调查分析及未来前景分析报告
  2. FilenameFilter的使用
  3. Cell综述:口腔微生物群的部位特点:微米级生境与生态位
  4. 源代码编译安装Apache2
  5. java 抽象类 final_Java8 final关键字与抽象类
  6. MPAndroidChart LineChart 折线图 你要的都在这里了
  7. python之禅怎么关闭_《Python之禅》中对于Python编程过程中的一些建议
  8. 按钮自动居中布局_CSS布局技巧
  9. jdk1.8要安装什么mysql_Window下安装JDK1.8+Tomcat9.0.27+Mysql5.7.28的教程图解
  10. vue环境搭建以及vue-cli使用
  11. innodb存储引擎监控
  12. 数据结构(C语言)-数组
  13. Mcmod模组下载脚本
  14. 塞班S40手机内存出现乱码无法删除的解决办法
  15. opencv 双目标定操作完整版
  16. Echarts实现中国地图线路图特效(一对多发射点)
  17. 关于《算法的乐趣》历法一章演示程序错误的说明
  18. 软件工程毕业设计课题(42)微信小程序毕业设计JAVA小说电子书小程序系统毕设作品项目
  19. 极客日报:宿华不再担任快手CEO,程一笑接任;微软市值重登全球第一;Bootstrap 4.6.1发布
  20. 多种数据库连接工具_20多种热门数据工具及其不具备的功能

热门文章

  1. Android背景渐变色(shape,gradient)
  2. msdn关于Visual C++ 编译器选项的说明
  3. 最强大的多线程解决方案!!
  4. 互联网思维-标签思维(1)
  5. JavaScript语言基础13
  6. RxSwift技术路线与参考资料
  7. macOS安装 cocoapods1.9.1失败Failed to build gem native extension
  8. Flutter开发之诊断布局调试工具:inspector(12)
  9. 多屏互动电脑版_MAXHUB无线双频同屏器HDMI连接投影仪手机电视机笔记本电脑投屏器多屏互动 MAXHUB传屏盒子WB01标配2个无线传屏...
  10. String.Format使用方法