Is there a way to use the onerror () attribute in <amp-img>
Can I use the onerror attribute in <amp-img>?
It worked well in html.
<img src="../images/some-logo1.jpg" onerror="this.src='../images/no-img.jpg';" class="posting-logo-img">
But amp html will create an img tag inside amp-img
<amp-img src="/img/amp.jpg" alt="AMP" class="posting-logo-img" onerror="this.src='../images/no-img.jpg';" >
<noscript>
<img src="/img/amp.jpg" alt="AMP">
</noscript>
</amp-img>
+4
1 answer
We cannot use the attribute onerrorin amp-img, but amp provides a Offline Fallbackfunction instead onerror.
amp-img supports common AMP attributes, which means you can show a backup if the image cannot be uploaded. This is great for adding offline support for your AMP.
404, , div:
<style amp-custom>
amp-img > div[fallback] {
display: flex;
align-items: center;
justify-content: center;
background: #f2f2f2;
border: solid 1px #ccc;
}
</style>
<amp-img src="/img/product-image.jpg" width="300" height="100" layout="responsive" class="m1">
<div fallback>offline</div>
</amp-img>
, (, no-image) , ,
<style amp-custom>
amp-img > div[fallback] {
background:url('/img/does-not-exist.jpg') no-repeat;
}
</style>
<amp-img src="/img/product-image.jpg" width="300" height="100" layout="responsive" class="m1">
<div fallback></div>
</amp-img>
. : Amp Image -
+6

