Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active June 20, 2016 13:08
Show Gist options
  • Save radist2s/1dcc0c253b126e3c410dde364e87fe59 to your computer and use it in GitHub Desktop.
Save radist2s/1dcc0c253b126e3c410dde364e87fe59 to your computer and use it in GitHub Desktop.
WEBP Support detector. Adding class to html element.
!function () {
function getWebpSupport() {
var canvas = document.createElement('canvas');
return canvas && canvas.getContext && canvas.toDataURL('image/webp').indexOf('data:image/webp') == 0
}
var webpSupport
if (document.cookie.indexOf('webp') == -1) {
webpSupport = getWebpSupport() && 1 || 0
document.cookie = 'webp=' + webpSupport + '; path=/'
}
else {
var webpMatch = document.cookie.match(/webp=(\d)/)
webpSupport = webpMatch && +webpMatch[1] || 0
}
if (webpSupport) {
document.documentElement.className += ' webp-supports'
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment