浏览器何时执行缓存中的 JavaScript?
我只是想知道当 JavaScript 来自缓存时浏览器在什么时候执行它。尽管我做了一些研究,但不知何故找不到任何令人满意的答案。
假设我有一个网站,我将所有 JS 放在一个文件中,并将其包含在结束正文标记之前。
现在,如果缓存为空,则在(大部分)内容下载完毕后加载 JS,然后执行(除非我使用延迟执行的方法)。
但是如果我转到下一页,JS就在缓存中。样式应用到 HTML 后会立即执行吗?在这种情况下,如果我没记错的话,用户界面可能会在内容下载过程中冻结。
希望我没有因为一些显而易见的事情而伤脑筋......
I was just wondering at what point the browser executes JavaScript when it comes from the cache. Even though I did some research, I somehow couldn't find any satisfactory answers.
Suppose I have a site, where I put all my JS in one file, which I include just before the closing body-tag.
Now, if the cache is empty, the JS gets loaded after (most of) the content has already been downloaded, and then executed (unless I'd use a method to defer execution).
But if I go to the next page, the JS is in the cache. Will it get executed right after the styles are applied to the HTML? In that case, the UI -might- freeze up during download of the content, if I'm not mistaken.
Hope I'm not wracking my brain over something obvious...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从网络下载或使用缓存应该会给出相同的结果。当浏览器看到第二页的script标签时,就会调用缓存并执行。如果您还在第二页上关闭 body 标记之前插入 script 标记,您将得到相同的结果。
Downloading from the web or using the cache should give you the same results. The moment the browser sees the script tag on the second page, it will call the cache and execute it. If you also insert the script tag right before closing the body tag on the second page, you will get the same result.
当它从
script
标签读取它时。因此,如果您将脚本标签放在那里,它仍然会等到到达页面底部。When it reads it from the
script
tags. So it will still wait until getting to the bottom of the page if you put your script tags there.