Skip to content

Instantly share code, notes, and snippets.

@b-aleksei
Created June 18, 2021 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-aleksei/1a10de79f11a5b08796634bf6fbe7ef9 to your computer and use it in GitHub Desktop.
Save b-aleksei/1a10de79f11a5b08796634bf6fbe7ef9 to your computer and use it in GitHub Desktop.
check-Format-Img
async function supportsImgType(type) {
const img = document.createElement('img');
await document.createElement('picture').append(
Object.assign(document.createElement('source'), {
srcset: 'data:,x', // валидный урл не дергающий сеть
type,
}),
img
);
return !!img.currentSrc; // если браузер умеет, заполнит значение currentSrc
}
for (const type of ['image/png', 'image/jpeg', 'image/webp', 'image/avif']) {
supportsImgType(type).then(supported => console.log(`${type}: ${supported}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment