Tuesday, October 20, 2015

Check if images are not rendered correctly to the browser using JavaScript

Hello,
Here's a function to hide divs if it's children specifically images does not render correctly to the browser or an error has occured.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
function ImageDivHandler() {    
   $("div.gallery").each(function () {  
     if ($(this).children().length == 0) {  
       $(this).hide();  
     }  
     else {  
       var img = $(this).find(".image-responsive");  
       if (img != null || img != undefined) {  
         imgSrc = $(this).find(".image-responsive").attr("src");  
         if (imgSrc == "" || imgSrc == null || imgSrc == "null") {  
           $(this).find(".image-responsive").closest("div.gallery").hide();  
         }  
         else {  
           $(this).find(".image-responsive").error(function () {  
             $(this).closest("div.gallery").hide();  
           });  
         }  
       }  
     }  
   });  
 }  
Note: I may change the logic from time to time depending on the layout of the image container. But the concept is pretty much the same.
Greg

0 comments:

Post a Comment