Skip to content

Instantly share code, notes, and snippets.

@ipatate
Created October 6, 2020 09:38
Show Gist options
  • Save ipatate/111d6a69d2b4ee011f75622f93a17b2c to your computer and use it in GitHub Desktop.
Save ipatate/111d6a69d2b4ee011f75622f93a17b2c to your computer and use it in GitHub Desktop.
Function for detecting support of webp
// thank http://jsfiddle.net/Schepp/uZDHY/ for idea
const canIUseWebp = () => {
return new Promise((resolve, reject) => {
// create image
const webp = new Image();
// detect if browser can read format
webp.onerror = () => reject('Your browser does not suppoert WebP');
webp.onload = () => resolve('Your browser supports WebP, yay!');
// just 1px blank webp image
webp.src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAsBMJaQAA3AA/veMAAA=';
});
};
// how to use
// canIUseWebp().then(e => console.log(e)).catch(err => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment