Skip to content

Instantly share code, notes, and snippets.

@ymatuhin
Last active May 8, 2021 13:20
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ymatuhin/a9601bd608b8de4e4dcc to your computer and use it in GitHub Desktop.
Check WebP Support in browser
function checkSupport(fn) {
var html = document.documentElement,
WebP = new Image();
WebP.onload = WebP.onerror = function() {
isSupported = (WebP.height === 2);
if (isSupported) {
if (html.className.indexOf('no-webp') >= 0)
html.className = html.className.replace(/\bno-webp\b/, 'webp');
else html.className += ' webp';
}
fn(isSupported);
};
WebP.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment