/
webp.js
34 lines (29 loc) · 863 Bytes
/
webp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(function (document) {
'use strict';
/**
* Test webP images support.
* @param {Function} callback - Callback function.
*/
var testWepP = function testWepP(callback) {
var webP = new Image();
webP.src = 'data:image/webp;base64,UklGRi4AAABXRUJQVlA4TCEAAAAvAUAAEB8wA' + 'iMwAgSSNtse/cXjxyCCmrYNWPwmHRH9jwMA';
webP.onload = webP.onerror = function () {
callback(webP.height === 2);
};
};
/**
* Add 'webp' class to body if supported.
* @param {Boolean} support - WebP format support.
*/
var addWebPClass = function addWebPClass(support) {
if (support) {
var el = document.body;
if (el.classList) {
el.classList.add('webp');
} else {
el.className += ' webp';
}
}
};
document.addEventListener('DOMContentLoaded', testWepP(addWebPClass(support)));
})(document);