How to tell when an image is already in browser cache in IE9?

*爱你&永不变心* 提交于 2019-12-01 21:03:48

I use this:

function doWhenLoaded(obj,callback) {

if($.browser.msie) {
    var src=$(obj).attr("src");
    $(obj).attr("src","");
    $(obj).attr("src",src);
}

$(obj).one("load",callback).each(function(){
    // alert(this.readyState+" "+this.src);
    if(
        this.complete
        || this.readyState == "complete"
        || this.readyState == 4
        || ($.browser.msie && parseInt($.browser.version) == 6)
    ) {
        $(this).trigger("load");
    }
}); 
}

A sample:

doWhenLoaded("#main_background_img",function(){
    $("#main_background_img").slideDown(1000);  
}); 

This is how i usually preload an image:

var img = new Image();
$(img).attr('src', "foo.jpg");
if (img.complete || img.readyState === 4) {
    // image is cached
    alert("the image was cached!");
} else {
    $(img).load(function() {
        // image was not cached, but done loading
        alert("the image was not cached, but it is done loading.");
    });
}

I haven't deeply debugged it in IE9, but I haven't ran into any issues with it.

the code was pulled from https://github.com/tentonaxe/jQuery-preloadImages/blob/master/jquery.preloadimages.js and modified.

You could try an AJAX request on the image and see the status code. If it's 304 it means the image was cached. Not sure how well that would work though. Maybe AJAX does some cache-busting.

I know this was asked a million years ago, but I figure I would contribute my solution which is similar but has less overhead and i/o.

Basically, you create a custom jQuery method that performs the similar feats all in one function:

$.fn.imgLoad = function(callback) {
    return this.each(function() {
        if(callback){
            if(this.complete || (this.readyState === 4) || (this.readyState === 'complete')) {
                callback.apply(this);
            } else {
                $(this).one('load.imgCallback', function(){
                    callback.apply(this);
                });
            }
        }
    });
}

This consolidates the checking of all possible events into one (both cached and uncached), but also makes the method chainable. You can call it with:

$('img').imgLoad(function(){
    console.log('loaded');
});

Works cross-browser, back to IE6. Notice it checks for caching first, and if not triggers a namespaced load event only once to prevent reloads in case you call the function with that logic twice, but also to allow custom load events to be bound (if applicable).

You can't tell if it's cached, but you can force a fresh load by "salting" the filename:

src:"http://farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg?"+new Date()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!