Home > Software engineering >  Fallback if image not found
Fallback if image not found

Time:12-07

If the file specified on an SVG <image> element is not found then the results are unpredictable. In Firefox, I get no indication and no error. In Chrome, I get some default box-shaped scenery image.

Is there a way to provide an alternative image, such as a nice error marker? I have seen solutions for a similar issue in HTML that involve the attribute:

onerror="this.onerror=null; this.src='Default.jpg'"

But this does not work in my SVG (even after changing src to href). I have also seen a cheat where using an additional src attribute provides a fallback for normal the href (see SVG Fallbacks) but it seemed to rely on a quirk of the implementation within the browser and no longer works (at least not in Firefox).

I'm looking for a method that works at least in Firefox, Chrome, and Edge.

CodePudding user response:

You want

this.href.baseVal='Default.jpg'

Due to SVG's support for SMIL

  • Related