-1

<img src="images/business-card.webp" onerror="this.onerror=null; this.src='images/business-card.jpg' " class="m-auto img-fluid">

I want to write this feature from within js file. How can i do it ?

spaceWalker
  • 139
  • 1
  • 10
  • There are no doubt better ways to achieve image fallback for webp. FOr example [this](https://stackoverflow.com/questions/65529016/webp-fallback-for-img-tag-in-html). If you can avoid writing code, your application will be more reliable. – Paul Rooney Oct 25 '21 at 03:17

1 Answers1

0

First, create the image in javascript:

var image = document.createElement("IMG");
document.body.appendChild(image);
image.src = "images/business-card.webp;
image.class = "m-auto img-fluid";

Now, add an onerror function:

blah.onerror = function(){this.onerror=null; this.src='images/business-card.jpg'};
user16791137
  • 111
  • 6