Results 1 to 3 of 3

Thread: replace broken/missing images

  1. #1
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default replace broken/missing images

    good day everyone,

    hi can anybody here wanna help me?

    i want to replace missing images automatically on load of the page,.

    i got some code on the net but it doesn't work on IE,. here's the code,.

    Code:
    for (var i=0; i<document.images.length; i++){
    
            img = new Image();
    
            img.src = document.images[i].src;
    
            if (img.height == 0)
    
                document.images[i].src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';
    
        }
    here's another one that is not workingon IE,.

    Code:
    $(window).load(function() {
    
      $('img').each(function() {
    
        if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0 || this.src=="") {
    
          // image was broken, replace with your new image
    
          this.src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';
    
        }
    
      });
    
    });

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,691
    Thanks
    1
    Thanked 252 Times in 247 Posts

    Default

    Hi there regicidedelferoz,

    I tested your first code in IE9, and it works OK.

    It will not work in IE8, and I assume IE6 and IE7 also, as those will browsers state that the height is 30px;

    I would suggest that you try..
    Code:
    
    for(var i=0;i<document.images.length;i++) {
        img=new Image();
        img.src=document.images[i].src;
    if(img.height<=30) {
       document.images[i].src='<?php echo $livesitePath; ?>images/picnotavailable.jpg';
       }
     }
    
    coothead

  3. #3
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    54
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    thanks for your reply sir,. but it seems that your code is not working fine sir,.

    i am using firefox 9.0.1 and IE 9


    but i already figure out the right code for jquery,.

    Code:
    $(window).load(function() {
    
      $('img').each(function() {
    
        if (!this.complete || this.naturalWidth == 0 || this.src=="") {
    
          // image was broken, replace with your new image
    
          this.src = '<?php echo $livesitePath; ?>images/picnotavailable.jpg';
    
        }
    
      });
    
    });

    and i think this is the error

    Code:
    typeof this.naturalWidth == "undefined"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •