Answer the question
In order to leave comments, you need to log in
How to write code that replaces the image in background-image with webp (so that pagespeed can see it)?
To determine if webp is supported by the browser, I use the JS code I took from this page:
https://developers.google.com/speed/webp/faq
I call it inside a construct like this:
$(function() {
...
});
Answer the question
In order to leave comments, you need to log in
Everything is simple. When the page is loaded, JS assigns the class "webp" or "no-webp" to the body (depending on whether webp is supported or not). background-image for an element is specified using cascading:
.webp .header-img {
background-image: url(../img-webp/header-img.webp);
}
.no-webp .header-img {
background-image: url(../img/header-img.png);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question