推迟加载js怎么做? [英] Defer loading of js how?

查看:50
本文介绍了推迟加载js怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面底部引用了以下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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆