I’m using javascript to dynamically load any of a series of images into a single img tag, based on user interaction:
function reassignImage(newSource)
{
img.src = newSource;
}
This works great, except that I when I inspect it with Chrome developer tools,
I see that even if I reload an image I’ve already loaded, it makes another http call AND grows the total Images Size graph.
This seems like the worst of both worlds. I would want either:
- To load from cache if the image were
the same. - To reload each image
everytime, but then not grow the
cache.
How would I achieve either scenario?
Thanks! Yarin
This will pre-load an image so that the browser can display it immediately when you actually set the
srcof animgtag. I speculate that pre-loading an image like this will ensure it’s in the cache so it won’t reload, though I haven’t tested it.In other words, this should now hopefully only download two images
Edit
I was doing it wrong. Try creating a
new Image()for every new file the user loads. Swap these image elements in and out of the dom.