Link to home
Start Free TrialLog in
Avatar of rossco11
rossco11

asked on

How to trap broken image links in javascript?

Hi,

I am trying to find a simple cross-browser javascript technique to detect when an image link is broken and replace it with a default picture on my page.  Has anyone been successful in doing this, and how is it possible?  

I believe that there is a onerror function when an image loads, however, i haven't been successful in implementing it.

Thanks!
Rossco
SOLUTION
Avatar of Psychotext
Psychotext

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Psychotext
Psychotext

Just to help a bit more, as you have a handle on the image in the function, you probably just want to change its location to the default file you mentioned.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rossco11

ASKER

thanks for the help, but how would i point it to the default image location?  i tried document.write, but that didn't work!
Could also use:

pic.src = '/images/replacement.gif'  

which would make things easier.
Just to say... that all looks about right.  I just didn't want to give everything away! ;-)
I never could keep a secret!
Thank you both, i really appreciate the quick response, i have got it to work now.  5 star service!!  
Just one thing to be very aware of - make sure that link is right and that it stays right forever!

If somebody comes to your page and that image has been deleted or moved or something then it'll cause you no end of problems, for example in your error logs that repeat the same thing over and over.

Anyway, glad we could help!
Ahh, then maybe I shouldn't be surprised to see the name of pepsichris at the top of the web development chart I can see on the left... :-)

Rossco11, you're welcome.
Me and my big mouth...
Hey,

This solution helped me out of a bind!

Thanks!