HTML OnMissingImage - With Dynamic Content

This morning Rob Gonda blogged about using the 'onerror' attribute of the <img> tag to display an alternate image if your image does not load. I tried this solution within a dynamic Spry region, and it didn't seem to work. I'm guessing that the onerror is fired once and only once, and since the image src is dynamic (within my Spry region) it is not firing the onerror again so the initial replacement image is loaded.

Thanks to some good discussion in the comments I devised a method to get this to work with my Spry region. It's pretty simple really, just register an observer on the region, and once the region is updated loop over the dataset, checking to see if the image is loaded. If not, replace it with your 'oops' image. Here's some code:

observer = function(type,notifier,data){
   if(type == 'onPostStateChange'){
      var ds = dsName.getData();
      
      for(var i = 0; i<ds.length; i++){
         //assumes you've named your image {ID}_img          
         var id = ds[i].ID + '_img';
         
         //check to see if the image was loaded          
         if(!document.images[id].complete){
             document.getElementById(id).src = 'oops.jpg';
         }
      }
   }
}
Spry.Data.Region.addObserver('regionName', observer);

That's it!

Comments

cfunited08

cfunited08

Calendar

Sun Mon Tue Wed Thu Fri Sat
    123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn sql

Recent Comments

Anyone Have A Copy Of Rebar?
369 said: Theres something similar on riaforge, maybe it meets some of what you're looking for? <a href=h... [More]

Applying Effects To CF Ajax Controls Revisited
Calvin said: Very Nice! How would one put cfwindow's name as a variable in to showWin and hideWin functions? T... [More]

Hosting Advice Needed
todd sharp said: Must have been on a box that I'm not on. Thank goodness too, because all of my sites (this blog, cf... [More]

Hosting Advice Needed
Oğuz Demirkapı said: No. And the tickets are still open in support system. :) I think they had a big outage and still ... [More]

Hosting Advice Needed
todd sharp said: Did they say what caused such a long outage? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com