javascript预加载图像-检查是否已缓存/加载图像以防止预加载 [英] javascript preloading images - check whether an image is cached/loaded to prevent preload

查看:115
本文介绍了javascript预加载图像-检查是否已缓存/加载图像以防止预加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些疑问,我已经确定了,但仍想非常精确.

如果这是一个重复的问题,请给我链接,以便我删除该问题并引用提供的链接

  1. 我需要预加载一些图像.如果它们已经存在于浏览器缓存中,我就不想对其进行预加载.

  1. I need to preload few images. I dont want to preload if they already exists in the browser cache.

如果我们正在预加载,那么我们将使用javascript代码进行预加载,以发送请求. 我们在html文档中也有一个img src用于相同的图像.浏览器将两次请求图像.一个是在预加载时,另一个是在看到img标签时?

if we are preloading then we use the javascript code to preload which sends a request. also we have an img src for the same image in the html document. will the browser request for an image two times. one while preloading and other when it sees the img tag?

我希望预加载和img src进程都是异步的.我希望链接像obj.src = path这样的图像时所占用的资源和请求将是相同的.

I hope both the preloading and img src processes are asynchronous. i hope that the resource and request taken when linking an image like obj.src = path and will be the same.

我希望预加载不能像同步Ajax一样工作.通过给予优先权

i hope preloading will not work like synchronous Ajax. that is by giving priority

是否可以知道图像是否在缓存中,这样我们就不必预先加载它.

is it possible to know whether an image is in cache so that we dont have to preload it.

我还想再问几个问题吗?如果是这样,请提供这些答案,以便堆栈溢出用户可以更新.

am i missing few more questions? if so then please provide those answers so that the stack overflow users could update.

是或否会很好.如果可能的话,我希望有一些指向外部参考或建议或答案的链接.

Just yes or no would be nice. if possible i would like to have some links to external reference or suggestions or an answser.

谢谢.

推荐答案

您可以检查.complete属性,如果图像已被缓存,则该属性应该大部分为true.

You can check the .complete attribute, it should mostly be true is image is cached.

如果在缓存它们时对其进行预加载,则不会造成太多的网络通信.您是否要节省内存?

If you preload them when they are cached, its not going to cause much network traffic.. Are you trying to save on memory?

您可以通过ajax发出head请求,如果它返回304而不是200,则意味着它在缓存中.

You can do a head request via ajax and if it returns 304 instead of 200, it would mean its there in the cache.

关于参考文献: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

这篇关于javascript预加载图像-检查是否已缓存/加载图像以防止预加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆