使用SCRIPT的defer属性

One of the seldom used attributes within the HTML tag library is the defer attribute on SCRIPT elements.  As you can probably tell by the name of the attribute, defer instructs the contents of the script tag to not execute until the page has loaded.  Take a look!

HTML标记库中很少使用的属性之一是SCRIPT元素上的defer属性。 您可以通过属性的名称看出来, defer指示脚本标记的内容在页面加载之前不执行。 看一看!

延迟脚本 (Deferring Your Scripts)


<script>
	//do stuff (runs first)
</script>
<script defer="defer">
	//do stuff, but defer it  (runs last)
</script>
<script>
	//do more stuff (runs second)
</script>


The deferred SCRIPT element's code will execute once the rest of the page's resources have loaded.  What does this mean?  Be sure that your document doesn't rely on any of the code within the script during page load.  In the example above, the middle block will execute once the page has loaded even though it appears before the last block.

页面的其余资源加载完毕后,将执行延迟的SCRIPT元素的代码。 这是什么意思? 确保页面加载期间,文档不依赖脚本中的任何代码。 在上面的示例中,即使页面出现在最后一块之前,中间块也会在页面加载后执行。

更多细节 (More Details)

Olivier Rochard has written an outstanding (and more detailed) post about using the defer attribute on the Mozilla Hacks blog.  His post details browser support (and quality of browser support...or lack thereof), advanced examples, and tips for using the defer attribute.

Olivier Rochard在Mozilla Hacks博客上撰写了一篇出色的(更详细的)关于使用defer属性的文章。 他的文章详细介绍了浏览器支持(以及浏览器支持的质量……或缺乏),高级示例以及使用defer属性的技巧。

What I find funny about this tag is that it seems as though most of the script I see should be using this attribute.

我对这个标签感到很有趣的是,似乎我看到的大多数脚本都应该使用此属性。

翻译自: https://davidwalsh.name/script-defer

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值