Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    OnError on Image

    I'm working on this piece of code on checking for the existence of an image, if not it will load another image...

    using onerror in an image, we can do that...
    Code:
    <img src=image.gif onerror="if(this.src.indexOf('replace.gif')==-1){this.src='replace.gif'}">
    the if(this.src.indexOf('replace.gif')==-1) is intended to prevent unending loops if ever replace.gif still doesnt exist

    but is there a way to check if onerror is true or false?

    since after loading the replace.gif image in replacement of image.gif....i want still to be sure that replace.gif does exist...because if not...it will load another final and certified existing image... so is there something like this that i can place like...

    if(this.onerror==true){this.src='real.gif'}

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by ken_shoti View Post
    the if(this.src.indexOf('replace.gif')==-1) is intended to prevent unending loops if ever replace.gif still doesnt exist
    Just have the handler cancel itself the first time it's called:

    Code:
    <img src='image.gif' onerror="this.onerror=null; this.src='replace.gif'">

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hm...didn't thought of that ) thanks....
    hm...but that onerror=null will not solve my main problem which is to check if replace.gif does exist or not...

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    var imgCtr=0;
    function loadNew(img){
      imgCtr++;
      if (imgCtr==1){
        img.src='replace.gif';
      }
      else{ //2nd error
        img.onerror=null;
        img.src='real.gif';
      }
    }
    Code:
    <img src="image.gif" onerror="loadNew(this);" />

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright...Ayos!
    Thanks for the code!

  • #6
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by ken_shoti View Post
    Alright...Ayos!
    Thanks for the code!
    That code limits you to one instance per document.
    Code:
    <img src='standard.gif' onerror='replaceImage(this, "backup1.gif", "backup2.gif")'>
    
    <script type='text/javascript'>
    
    function replaceImage(ref,img1,img2)
    {
     ref.onerror=new Function("this.onerror=null;this.src='"+ img2 +"'");
     ref.src=img1;
    }
    
    </script>
    When testing locally, you will probably find that Mozilla and Opera don't fire the onerror event.

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    nice...thnaks for the addition


  •  

    Posting Permissions

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