Javascript图像预加载策略

发布于 2024-09-26 07:07:48 字数 410 浏览 4 评论 0原文

像许多人一样,我长期以来一直在进行图像预加载,但并不总是非常理性。因此,我想出了这个关于使用 javascript 预加载图像的正确方法的简短想法列表。

  • 应尽快执行图像预加载脚本。它应该放置在 HTML 的顶部(在 HEAD 部分),与其他位于 BODY 部分底部的脚本不同。
  • 它不能依赖于库(例如 jQuery),因为这会延迟执行。
  • CSS 精灵和背景图像不需要预加载,因为 CSS(通常在 HTML 顶部调用)已经完成了这项工作(这当然减少了 javascript 图像预加载的总体需求)。
  • 预加载脚本可以在站点的每个页面上放置并运行,以确保无论用户进入站点的哪个页面都有效。 (但是,如果只是为了检查图像现在是否已缓存,那么每次运行脚本的开销又如何呢?)

我很想听听您对这个主题的看法。

Like many people I've been doing image preload for a long time, but not always very rationally. So I've come up with this short list of thoughts about the right way to preload images with javascript.

  • An image preload script should be executed as soon as possible. It should be placed at the top of the HTML (in the HEAD section), unlike the others scripts that go to the bottom of the BODY section.
  • It must not rely on a library (such as jQuery) because that would delay the execution.
  • CSS sprites and background-images need not be preloaded because the CSS - normally called at the top of the HTML - already does the job (this of course reduces the overall need for javascript image preload).
  • The preload script can be placed and run on every page of the site so as to be sure it is effective no matter which page the user enters the site. (But what about the overhead of running the script every time, if only to check the images are now cached?)

I'd be very interested to hear your opinions on this subject.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-03 07:07:48

应尽快执行图像预加载脚本。它应该放置在 HTML 的顶部(在 HEAD 部分),与位于 BODY 部分底部的其他脚本不同。

这会优先考虑用户执行某些操作时可能显示的图像,然后优先考虑作为初始页面一部分的图像。 (除非您尝试预加载属于初始页面一部分的图像,否则这是多余的)。

它不能依赖于库(例如 jQuery),因为这会延迟执行。

这样的延迟应该不会很严重

CSS 精灵和背景图像不需要预加载,因为 CSS(通常在 HTML 顶部调用)已经完成了这项工作(这当然减少了 javascript 图像预加载的总体需求)。

如果样式表中提到的图像未显示,浏览器可能会优化并且不会加载它们。

预加载脚本可以在网站的每个页面上放置并运行,以确保无论用户进入网站的哪个页面都有效。 (但是,如果只是为了检查图像现在是否被缓存,那么每次运行脚本的开销又如何呢?)

合理的缓存控制标头应该避免需要发出 HTTP 请求来检查图像的新鲜度,因此这应该可以忽略不计。

An image preload script should be executed as soon as possible. It should be placed at the top of the HTML (in the HEAD section), unlike the others scripts that go to the bottom of the BODY section.

This puts greater precedence on images that might be shown if the user does something then on images which are part of the initial page. (Unless you are trying to preload images which are part of the initial page, which would be redundant).

It must not rely on a library (such as jQuery) because that would delay the execution.

Such a delay shouldn't be significant

CSS sprites and background-images need not be preloaded because the CSS - normally called at the top of the HTML - already does the job (this of course reduces the overall need for javascript image preload).

Browsers may optimize and not load images mentioned in stylesheets if they aren't being displayed.

The preload script can be placed and run on every page of the site so as to be sure it is effective no matter which page the user enters the site. (But what about the overhead of running the script every time, if only to check the images are now cached?)

Sensible cache control headers should avoid the need to make HTTP requests to check the freshness of images, so this should be negligible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文