jQuery .load()没有触发图像(可能是缓存?) [英] jQuery .load() not firing on images (probably caching?)

查看:166
本文介绍了jQuery .load()没有触发图像(可能是缓存?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常基本的jQuery代码:

I have some pretty basic jQuery code:

...
$(this).find('img').load(function(){
   loadedImages++;
   if(loadedImages == $this.find('img').length){
...

然而,这不会持续发射。如果我进行硬刷新或关闭浏览器,但正常刷新,或者只需在不擦除缓存的情况下两次点击相同的URL就会使 .load()永远不会触发。

However, thats not firing consistently. It fires if i do a hard refresh or close my browser, but a normal refresh, or just hitting the same URL twice at any time without erasing the cache makes the .load() never fire.

关于如何解决这个问题的任何想法?

Any ideas on how to fix this?

推荐答案

一种方法是在URL的末尾添加虚拟变量用于获取图像的...例如作为查询字符串参数附加的以毫秒为单位的时间。

A way would be to add a "dummy variable" at the end of the URL that you use to grab the image... such as the time in milliseconds appended as a query string param.


编辑: 在99%的情况下,防止浏览器缓存图像是一个非常糟糕的主意,因为它会降低应用程序的速度。相反,您应该检查图像是否已经加载,如果没有等待加载要完成的事件。

Preventing the Browser to Cache images is a very bad idea in 99% of the cases as it will slow down your application. Instead you should check if an image is already loaded, and if not wait for the load event to complete.

这篇关于jQuery .load()没有触发图像(可能是缓存?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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