简体   繁体   English

JavaScript图像预加载无法正常工作

[英]Javascript image preload doesn't correctly work

I have a JS/jQuery scripts which appends images dynamically on page. 我有一个JS / jQuery脚本,可以在页面上动态添加图像。 I am using node as server. 我正在使用节点作为服务器。 I've added logs server side and I noticed that everytime there is an "append" done, the page asks the server for the image, even if it is always the same two images which are appended. 我已经在服务器端添加了日志,并且我注意到每次完成一个“附加”操作后,页面都会向服务器询问该图像,即使它始终是相同的两个图像。 Now, this happens on Chrome, but not on Mozilla, where the images are loaded only once. 现在,这会在Chrome上发生,但不会在Mozilla上发生,因为Mozilla仅将图像加载一次。 I thought this would be the cache; 我以为这将是缓存; but cache isn't disabled. 但并未禁用缓存。 Also, I've tried doing some preloading by creating an Image object with the right src in the beginning of the page : same problem. 另外,我还尝试通过在页面开头使用正确的src创建一个Image对象来进行一些预加载:同样的问题。 Even if it is a browser cache setting, I don't want to build a website where users with cache disabled spam my server with useless requests of the same images... Any advice ? 即使这是浏览器的缓存设置,我也不想建立一个网站,在该网站上禁用缓存的用户向我的服务器发送垃圾邮件,而这些请求对相同的图像无用。

You should also try/research preloading images with other techniques too if it fits to your requirements and if JS preloading is too complicated. 如果适合您的需求并且JS预加载太复杂,您还应该尝试/研究使用其他技术预加载图像。 Good discussion is found here 在这里找到很好的讨论

http://perishablepress.com/pure-css-better-image-preloading-without-javascript/ http://perishablepress.com/pure-css-better-image-preloading-without-javascript/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM