Created
October 6, 2020 09:38
-
-
Save ipatate/111d6a69d2b4ee011f75622f93a17b2c to your computer and use it in GitHub Desktop.
Function for detecting support of webp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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