javascript 预加载图像 - 检查图像是否被缓存/加载以防止预加载
我有一些疑问,我已经确定,但仍然想非常精确。
如果这是重复的问题,请给我链接,以便我删除这个问题并将引用提供的链接
我需要预加载一些图像。如果它们已存在于浏览器缓存中,我不想预加载。
如果我们要预加载,那么我们使用 JavaScript 代码来预加载发送请求。 我们在 html 文档中也有相同图像的 img src。浏览器将请求图像两次。一个是在预加载时,另一个是在看到 img 标签时?
我希望预加载和img src进程都是异步的。我希望链接像 obj.src = path 这样的图像时所采取的资源和请求是相同的。
我希望预加载不会像同步 Ajax 那样工作。也就是说,通过给予优先级
,是否可以知道图像是否在缓存中,以便我们不必预加载它。
我还漏掉了几个问题吗?如果是这样,那么请提供这些答案,以便堆栈溢出用户可以更新。
只要是或否就好了。如果可能的话,我希望有一些外部参考或建议或答案的链接。
谢谢。
I have a few doubts which i am already sure of it but still want to be very precise about it.
If this is a duplicate question then please give me links so that i will delete this question and will refer the provided links
I need to preload few images. I dont want to preload if they already exists in the browser cache.
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?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.
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.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查 .complete 属性,如果图像已缓存,它应该大部分都是正确的。
如果您在缓存它们时预加载它们,则不会导致太多网络流量。您是否想节省内存?
您可以通过 ajax 执行 head 请求,如果它返回 304 而不是 200,则意味着它在缓存中。
关于参考资料:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
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?
You can do a head request via ajax and if it returns 304 instead of 200, it would mean its there in the cache.
Regarding References :
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
“我需要预加载一些图像。如果它们已存在于浏览器缓存中,我不想预加载”。 没关系,预加载是为图像对象的 src 属性分配一个 url,然后它会完成其余的工作,如果图像已经可用,则请求下一个图像。
"I need to preload few images. I dont want to preload if they already exists in the browser cache". It does not matter, preloading is assinging a url for the src property of the image object and it does the rest, if the image is already availble the next image is requested.