推迟加载js怎么做? [英] Defer loading of js how?
问题描述
我在页面底部引用了以下js脚本:
I have the following js script referenced on the bottom of the page:
<script src="http://example.com/test.js" type="text/javascript"></script>
Google PageSpeed建议是推迟加载此js.我不太了解该怎么做或产生的影响.有人可以解释吗?
Google PageSpeed suggestion is to defer the loading of this js. I don't quite understand how to do that or the repercussions. Can someone please explain?
推荐答案
将属性 defer
添加到< script>
标记即可.例如:
Adding the attribute defer
to the <script>
tag should do it. E.g.:
<script src="http://example.com/test.js" type="text/javascript" defer></script>
这个想法是,文件中的脚本仅在整个页面加载完成后才执行,这与浏览器在解析< script>脚本后立即执行脚本的标准方式相反.
标签(可能会延迟< script>
标签之后的代码呈现.)
The idea is that the script in the file is executed only after the whole page has finished loading, as opposed to the standard way when the script is executed as soon as the browser parses the <script>
tag (which may delay rendering of the code after the <script>
tag.)
这篇关于推迟加载js怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!