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:
那么,您即可按如下這樣內(nèi)嵌腳本:
這樣,您就可以將small.jsEmbedded in the HTML document, thus eliminating external requests for it.
為防止JavaScript阻止網(wǎng)頁(yè)加載,建議您在加載JavaScript時(shí)使用HTML異步屬性。例如:
如果您的JavaScript資源使用的是document.write,則使用異步加載就會(huì)不安全。我們建議您重寫(xiě)使用document.write的腳本,以改用其他技術(shù)。
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庫(kù)(例如jQuery),該怎么做?
- 很多JavaScript庫(kù)(例如jQuery)都可用來(lái)增強(qiáng)網(wǎng)頁(yè),從而為網(wǎng)頁(yè)增添額外的互動(dòng)性、動(dòng)畫(huà)和其他效果。不過(guò),這些行為大多可在首屏內(nèi)容呈現(xiàn)后再安全添加。請(qǐng)考慮是否可以將此類(lèi)JavaScript的執(zhí)行和加載延遲到網(wǎng)頁(yè)加載后。
- 如果我使用JavaScript框架來(lái)構(gòu)造網(wǎng)頁(yè),該如何操作?
- 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.
