0
I am using this script to check image support webp
but I’m having trouble defining the type, here’s the code that checks:
hasWebP = (function() {
var images = {
basic: "data:image/webp;base64,UklGRjIAAABXRUJQVlA4ICYAAACyAgCdASoCAAEALmk0mk0iIiIiIgBoSygABc6zbAAA/v56QAAAAA==",
lossless: "data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA="
};
return function(feature) {
var deferred = $.Deferred();
$("<img>").on("load", function() {
if(this.width === 2 && this.height === 1) {
deferred.resolve();
} else {
deferred.reject();
}
}).on("error", function() {
deferred.reject();
}).attr("src", images[feature || "basic"]);
return deferred.promise();
}
})();
hasWebP().then(function() {
tipo = "webp";
}, function() {
tipo = "jpg";
});
console.log(tipo);
But when I call the variable tipo
outside this function it is given as undefined
.
I’ve tried to raise her like this window.tipo
but it didn’t work either, how can I solve this problem?
When in my script can I set the type of image to be used? From what I understood right here with your suggestion, if I call the variable 'type' somewhere in the script, it continues as Undefined
– Leo Letto
@Leoletto there needs to assess the scenario. But surely, it is not with global variables that the problem is solved, since its state will depend on the execution of other asynchronous resources.
– Wallace Maxters
It is that I want to do this to check what kind of image I should use, such images will be added to the site through a loop, so I thought it would be possible, but since it is an asynchronous call, I think it is worth it to work with this direct detection in htaccess, or in PHP itself while loading the page
– Leo Letto