如果 JavaScript 位于 HTML 页面的 body 标记中,是否可以缓存?
我正在阅读这篇如何使 HTML 渲染速度更快,上面写着可以缓存 HEAD
标记中的脚本。
BODY
标签中的 JavaScript 可以缓存吗?如果不是,为什么 YUI 建议将脚本放在 body 标签中?
I am reading this How to make HTML rendering fast it says that scripts in the HEAD
tag can be cached.
Can JavaScript in the BODY
tag be cached? If not, why does YUI recommend putting scripts in the body tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 JavaScript 位于外部文件和缓存控制标头中,则 JavaScript 将被缓存(并在页面之间重用)说它应该被缓存。
如果它嵌入在页面本身中(即在
和
之间而不是在
src= 的末尾,则可能会被缓存) “...”
),但前提是整个页面都被缓存,并且它不能在页面之间重用。如果
位于头部或正文中,那么对于缓存来说没有什么区别。
JavaScript will be cached (and reused between pages) if it is in an external file and the cache control headers say it should be cached.
It may be cached if it is embedded in the page itself (i.e. between
<script>
and</script>
instead of at the end ofsrc="..."
), but only if the entire page is cached and it will not be reusable between pages.It makes no difference, to caching, if the
<script src="..."></script>
is in the head or body.如果缓存整个 HTML 页面,代码将被缓存,否则不会。如今,HTML 页面通常是动态的(由脚本和 CGI 生成),因此不可能在不牺牲功能的情况下进行缓存。因此,您通常希望将 JS 代码放在外部文件中,然后可以通过为 JS 文件设置 HTTP 缓存标头来缓存这些代码。
大多数情况下,答案是您无法缓存内联到 HTML 代码中(在 HEAD 部分或其他部分)的 JavaScript。为了使其可缓存,您需要将其放入外部文件中,但是浏览器将需要执行额外的 HTTP 请求才能首次获取 JavaScript。
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.
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.
伙计,我想你可能误解了里奇的话。
他说将 JavaScript 放入外部文件中并从头部链接它。
这与将 JavaScript 放入页面正文中的脚本标记形成对比。
如果 JavaScript 仅在该页面上使用,则将 JavaScript 放入页面正文中的脚本标记中是合理的。事实上,如果它只使用该页面的一个,那么将其放入外部文件中并不是一种优化。对 JS 文件的额外 GET 请求在 Firefox、Opera、Safari 上几乎同时发生,但在 IE6 上不会。原因是 IE6 只有几 (2) 个线程用于获取文件,而 Firefox 则多达 16 个。这就是为什么为页面特定代码建立一个单独的文件将是一种倒退因为它实际上可能会减慢页面加载速度。
但是,如果您有一个想要在多个页面上使用的通用 JavaScript 文件,那么您绝对应该将其放置在外部文件中并从头部链接它,因为第一次加载任何这些页面时它将被缓存,并且当任何其他页面使用它时,不需要再次获取它。文件越大,缓存它的优势就越大。
我认为这就是他所要表达的观点。这有帮助吗?
Mate, I think you might have misunderstood what Rich said.
He said put the JavaScript into an external file and link it from the head.
This is in contrast to placing the JavaScript into a script tag in the body of the page.
It would be reasonable to put JavaScript into a script tag in the page body if it is used only on that page. In fact, if its used only one that page, it would not be an optimisation to place it into an external file. The additional GET request for the JS file will be almost simultaneous on Firefox, Opera, Safari but not on IE6. The reason is that IE6 has only a few (2) threads to use for fetching files, whereas Firefox has up to 16. That is why having a separate file for page-specific code would be a step backwards because it might actually slow down the page load.
If however, you have a common JavaScript file that you want to use on many pages then you should definitely place it in an external file and link it from the head, because it will be cached the first time any of those pages are loaded, and it will not need to be fetched again when any of the other pages use it. The bigger the file, the bigger the advantage to caching it.
I think that was the point he was making. Does that help?