Forum How do I...?

How to use the onerror behaviour of an img tag

tobykemp
I am trying to run the following, but it isn't working and i am wondering if the 'onerror' is valid to use as i am trying to do in Prince, or if its my syntax that is incorrect.

Basically i'm trying to say that if the image fails to load then change the image src path to use a different directory.

<img src="http://www.imageserver.com/live/image.jpg" onerror="if (($this).src.indexOf('/test/') == -1) ($this).src=($this).src.replace('/live/','/test/')"/>
mikeday
Prince does not support this JavaScript event. But you can use fallback in CSS like this:
img { content: prince-fallback(url("a.jpg")), url("b.jpg") }

This isn't dynamic, but if you stored the alternate fallback image URL in another attribute, eg. data-altsrc, then it would work:
img { content: prince-fallback(attr(src, url)), attr(data-altsrc, url) }
tobykemp
Mike,

Brilliant solution mate. That worked an absolute treat.

Thanks.