views:

334

answers:

3

I'm dynamically switching background images. Naturally, they need to be preloaded to display promptly. I am preloading them, am able to following in FireBug as the images load. When the background image switches, I see the images download again in FireBug.

Here's my url: http://www.morganpackard.com/siteRoot/

Strangely, if I reload the page, everything works as expected. The steps I'm taking are as follows:

1) clear FireFox cache 2) reload page Images load slowly, and, apparently are neither being preloaded nor cached. Every time there's an image swap, the image downloads again. 3) now reload the page again without clearing the cache Images load and swap instantly, everything works as expected.

So it looks like I'm having to RELOAD before FireFox will read images from the cache. This is kooky. Any ideas?

Note: My images are ENORMOUS. I'll compress them, of course, but want to get this preloading this sorted first.

+1  A: 

Checking with HttpFox, I am not seeing further http requests - when clicking around - for your background images after the initial load. If I hit "refresh" then the images are reloaded. I think this is correct and what you'd expect. However be aware that reloading of a page and its assets like images, css etc on a page refresh is dependent on both response headers and any over-rides for these specified in your browser/plugin tools. Take a look at pragma: no-cache and W3C Cache-Control (Section 14.9)

Edit: With firebug on, I am seeing reloads when I click around. This is presumably an artefact of Firebug. Turn off firebug and you wont have the reloads.

Richard
I've turned FireBug off, and the images are still loading slowly. Works as expected in Safari, but not firefox.
morgancodes
A: 

I didn't read your code, but for preloading to work, you must add the image to the document DOM, otherwise it can be garbage collected aggressively.

Hmmm. But the classic JS preload just creates JS image objects, without adding them to the DOM. Just to make sure, I tried literally appending my images to the dom, making them visible. So I see ImageA on the screen, obviously loaded in the DOM, but when I set the background to ImageA, there's a pause while it downloads again.
morgancodes
I think Nicholas is correct; I typically add preloaded images to DOM elements removed from view.
Upper Stage
When you set your background-image, are you sure you use the same url than the image you just preloaded? You might try using image.src (as image is the one you preloaded), also be sure to do that when the image is loaded.Otherwise I don't know.
Thanks guys, yeah, everything you've suggested checks out. I added a comment to my original question outlining an additional strangeness -- that my images are read correctly from the cache AFTER I reload.
morgancodes
A: 

mogancodes, I am having the exact same issue in Firefox 3.5.5. I clear the cache and load the page and the preloaded images keep downloading every time. If I then reload the page without clearing the cache, everything works perfectly. However I am NOT seeing this problem in any other browsers INCLUDING Firefox 3.0.13 and Firefox 2.0.0.20. Looks like a bug in Firefox 3.5 to me. Have you been able to make any progress on the problem?

Neal Ferrazzani
UPDATE: This only seems to be happening when I do a SHIFT+reload. morgancodes, the first time I hit your site, the preloading worked beautifully in FF 3.5.5, however if I SHIFT+reloaded (which I normally use to clear the FF cache) I had the same issue with preloading not working. I think this is just a problem with SHIFT+reload in FF 3.5.5. I disabled the cache using Web Developer Toolbar plugin and everything is working perfectly.
Neal Ferrazzani
Thanks Neal, you're totally right. Looks like a FireFox bug to me.
morgancodes