I'm loading an image in an iframe and then (once the iframe is loaded) loading the image on the page. But most browsers seem to be loading the image twice. Why isn't the img
tag being loading from the cache?
Something like this:
var loader = $('<iframe />').appendTo('body')[0];
loader.onload = function() {
$('body').append('<img src="' + imgsrc + '" />');
};
loader.src = imgsrc;
http://jsfiddle.net/amirshim/na3UA/
I'm using fiddler2 to see the network traffic.
In case you want to know why I want to do this, check out this question
Having an iframe is like having a separate tab, it uses another browser instance, for this reason I believe it is not using the cache.
Still if what you want is having the picture pre loaded, you can use
new Image()
in javascriptI have tested with this new version and it works, caching is used for the second image, tested on IE7 & FF3.6. the difference is in the way i set the img src attribute in the load() jQuery callback (but why remains a mystery for me).
Be careful in this example I used a huge image to really see the difference.
edit fiddle link: http://jsfiddle.net/regilero/g8Hfw/
Maybe you sending HTTP headers, which prevent the caching? If you use PHP to send the image to the user and start a session, PHP will set headers forcing reloading the image all the time.
.htaccess can be used to improve caching and you can set headers in PHP too. I use .htaccess: if the image has been loaded once and you've made no change, it will be loaded from cache. This behavior can be set for other types of files (see below):
.htaccess