If external scripts are small, you can add them directly to HTML documents. Embedding smaller files in this way allows the browser to continue rendering web pages. For example, if the HTML document looks like this:
资源small.jsAs shown below:
那么,您即可按如下这样内嵌脚本:
这样,您就可以将small.jsEmbedded in the HTML document, thus eliminating external requests for it.
为防止JavaScript阻止网页加载,建议您在加载JavaScript时使用HTML异步属性。例如:
如果您的JavaScript资源使用的是document.write,则使用异步加载就会不安全。我们建议您重写使用document.write的脚本,以改用其他技术。
Also, when loading JavaScript asynchronously, if your Web page loads scripts that depend on each other, be careful to ensure that your application loads the scripts in the proper order of dependencies.
- 如果我使用的是JavaScript库(例如jQuery),该怎么做?
- 很多JavaScript库(例如jQuery)都可用来增强网页,从而为网页增添额外的互动性、动画和其他效果。不过,这些行为大多可在首屏内容呈现后再安全添加。请考虑是否可以将此类JavaScript的执行和加载延迟到网页加载后。
- 如果我使用JavaScript框架来构造网页,该如何操作?
- If your Web page content is constructed from client-side JavaScript, you should consider whether you can embed related JavaScript modules to avoid additional network round trips. Similarly, using server-side rendering can significantly improve the effect of the first web page loading by rendering the JS template on the server, embedding the results into HTML, and then using the client template after the application is loaded. For details on server-side rendering, seehttp://youtu.be/VKTWdaupft0? t = 14m28s。
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under theApache 2.0 License. For details, see ourSite Policies.
