如果JavaScript在HTML页面的主体标签中,它可以被缓存吗?
我正在阅读这篇如何快速呈现HTML 它表示可以缓存 HEAD
标记中的脚本。
I am reading this How to make HTML rendering fast it says that scripts in the HEAD
tag can be cached.
JavaScript中的 BODY
标签被缓存?如果不是,YUI为什么推荐将脚本放在body标签中?
Can JavaScript in the BODY
tag be cached? If not, why does YUI recommend putting scripts in the body tag?
如果缓存整个HTML页面,而不是其他。 HTML页面现在通常是动态的(由脚本和CGI生成),因此无法在不牺牲功能的情况下进行缓存。因此,您通常希望将JS代码放置在外部文件中,然后通过为JS文件设置HTTP缓存标头来缓存外部文件。
The code will be cached if you cache the entire HTML page, not otherwise. HTML pages are usually dynamic these days (generated by scripts and CGIs) and therefore not possible to cache without sacrificing functionality. Therefore you usually want to place JS code in external files which then can be cached by setting HTTP cache headers for the JS file.
答案大部分是您无法缓存内嵌到HTML代码中的JavaScript(在HEAD部分或其他部分)。为了使其可缓存,您需要将其放入外部文件中,但浏览器需要额外执行HTTP请求才能第一次获得JavaScript。
The answer, for the most part, is that you cannot cache JavaScript which is inlined into the HTML code (in the HEAD section or otherwise). To make it cacheable you need to put it in an external file, but then the browser will need to do an extra HTTP request to get the JavaScript the first time.