Detecting WebP support
Easily check for WebP image format support in your browser with this neat JavaScript snippet:
Running this code will quickly test WebP support in the current browser.
In-depth explanation
Understanding the quick test
Let's decode our quick test:
- An
Image
object is created - The
onload
andonerror
event handlers are defined — one of them will fire (depending on support) - The
src
attribute is set to a base64-encoded WebP image - When the image loads, WebP support is confirmed (or denied), and it logs the support status
The canvas technique: supporting older browsers
Canvas gives us an alternative to WebP testing. Use elem.getContext()
, to test browser canvas support:
If there's no canvas support, alternative methods like Modernizr or more traditionally, <picture>
elements, may come handy.
Leverage HTML5 picture element
Utilize HTML5's <picture>
element to provide alternative formats in case of no WebP support:
It's like giving a straight-A student their golden star and a sweet kid their candy.
Optimizing with caching
Remember the old adage "Don't repeat yourself (DRY)"? We cache the WebP support detection result to save computational resources. Once you've checked, don't keep checking – just remember it:
Your CPU will thank you!
Deep dive into WebP features
WebP has several different features (like 'lossy', 'lossless', 'alpha', and 'animation'). You can check for each using specific test images. Get as specific as you wish.
Managing callbacks and async behaviour
As with all things JavaScript, we're living in asynchrony. You can either use callbacks or the more modern Promise to manage it when detecting WebP support:
Advanced techniques
Handling async image loading
For more advanced applications, apply asynchronous image loading to prevent blocking:
Browser quirks
Remember, not all browsers are created equal! 🦓 Firefox 65, for example, has issues with synchronous WebP detection. Be mindful of these small gotchas when writing your detection code.
Accessibility is key
WebP support does not replace good habits. Always include an alt
attribute for your images to ensure users with screen-readers can have a delicious web experience:
Was this article helpful?