Created
March 7, 2017 16:31
-
-
Save jcward/87200ee214fd406414c686f3f7e1d998 to your computer and use it in GitHub Desktop.
Quick webp support detection
This file contains hidden or 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
| // Based on Modernizr webp basic test: | |
| // https://github.com/Modernizr/Modernizr/blob/5eea7e2a213edc9e83a47b6414d0250468d83471/feature-detects/img/webp.js | |
| // | |
| // - Adds webp / no-webp classes to <body>, allowing loading of background images, e.g. scss: | |
| // | |
| // body.no-webp .hero-space { background-image: url(".../hero-space.jpg"); } | |
| // body.webp .hero-space { background-image: url(".../hero-space.webp"); } | |
| // | |
| // - TODO: <img> lazyloading support | |
| (function() { | |
| function handler(event) { | |
| var result = event && event.type === 'load' ? image.width == 1 : false; | |
| document.body.className += result ? " webp" : " no-webp"; | |
| } | |
| var image = new Image(); | |
| image.onerror = handler; | |
| image.onload = handler; | |
| image.src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA='; | |
| })(); | |
| // Minified: | |
| // (function(){function A(A){var a=A&&"load"===A.type?1==o.width:!1;document.body.className+=a?" webp":" no-webp"}var o=new Image;o.onerror=A,o.onload=A,o.src="data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA="})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment