Skip to content

Instantly share code, notes, and snippets.

@Protoff
Last active December 28, 2023 23:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Protoff/d6643387f03d47b44b2d7c3cf7b3e0a0 to your computer and use it in GitHub Desktop.
Save Protoff/d6643387f03d47b44b2d7c3cf7b3e0a0 to your computer and use it in GitHub Desktop.
Тест браузера на поддержку webp
document.addEventListener('DOMContentLoaded', function() {
testWebP(document.body)
})
function testWebP(elem) {
const webP = new Image();
webP.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA';
webP.onload = webP.onerror = function () {
webP.height === 2 ? elem.classList.add('webp-true') : elem.classList.add('webp-false')
}
console.log(webP)
}
@Protoff
Copy link
Author

Protoff commented Apr 27, 2020

Такой тест нужен, если изображение задаётся через css. Этот код просто добавит нужный класс в зависимости от результата. Так я делаю фон с webp

@ASanovS
Copy link

ASanovS commented Dec 28, 2023

Дякую дуже поміг!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment