1

I've been going at this a few days now and need help. I know that the .load() event doesn't get called if the image is cached in Safari so I came up with the code below.

It works in telling if the image is cached or not but it can't get the cached image's width and height. It comes up zero (in Safari). Any insight would be appreciated. Thanks.

         var $_item = $('<div id="'+(name+itemIndex)+'" class="box" ><div class="image_back"><img src="'+ image_back +'" id="backcolor" /></div><div class="image_outline"><img src="'+ image_outline +'" /></div></div>').appendTo('body');

         if( $('#backcolor', $_item)[0].complete ) {
            el_w = $('#backcolor', $_item).width();
            el_h = $('#backcolor', $_item).height();
            console.log("cache*****" +":"+el_w+":"+":"+$('#backcolor', $_item)[0].width);

        } else {

            $('#backcolor', $_item).load( function() {
                el_w = $('#backcolor', $_item).width();
                el_h = $('#backcolor', $_item).height();
                console.log("load*****" + this+":"+el_w);

            });


        }
Mistergreen
  • 1,052
  • 1
  • 8
  • 16
  • Try wrapping the code that is getting width and height on a cached image with `setTimeout(function(){/* get width height here */},0)`. Also, what version of jQuery? are these elements visible? – Kevin B Jan 02 '13 at 20:40
  • Have you tried disabling caching with jquery? `$.ajaxSetup({cache: false});` – Ulises Jan 02 '13 at 20:41
  • 1
    @Ulises That has nothing to do with the load event. – Kevin B Jan 02 '13 at 20:43
  • @KevinB load event? did you mean method? I know it prevents the browser from caching requests (see http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached). – Ulises Jan 02 '13 at 20:46
  • @Ulises Yes, load event. `.load` has a dual purpose (until one of those purposes are removed in a future version of jquery). One is the bind to the load event, the other is to send an ajax request and append it's contents to the selected element. In this case, we're binding to the load event. – Kevin B Jan 02 '13 at 20:50
  • @KevinB, thanks for clarifying I went over the code too quickly. – Ulises Jan 02 '13 at 20:54
  • Thanks, adding the setTimeout work. What a hack but it works :) – Mistergreen Jan 02 '13 at 21:15

0 Answers0