skip to Main Content

I have been reworking a web site to optimize for SEO. One thing I did is to optimize Images using Googles Webp Format. But Webp is not supported on any version of Safari.

My question is: What can I do to load one image (Webp) if supported or load another like JPG if not supported.

It could be great if it is something like the Audio or Video TAG

<audio controls>
   <source src="horse.ogg" type="audio/ogg">
   <source src="horse.mp3" type="audio/mpeg">
   Your browser does not support the audio tag.
</audio>

2

Answers


  1. Add your code like as below and it should most probably work.

    <picture>
        <source srcset="image.webp" type="image/webp">
        <source srcset="image.jpg" type="image/jpeg">
        <img src="image.jpg">
    </picture>
    

    More details are mentioned here in this link.

    Login or Signup to reply.
  2. I assume you are using only 1 image in src (in img tag) if that fails to load then you are trying to loan any other image. So, You can use

    <img src="invalid_link" onerror="this.onerror=null;this.src='other_image_url';">
    

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search