i would like to know how i could add image attributes to the following code. I've added a teest function to see if the users browser supports webp image and if it doesn't it just shows a jpg image instead of webp but i want to add attributes and nothing i've done works.
function hasWebP() {
var rv = $.Deferred();
var img = new Image();
img.onload = function() { rv.resolve(); };
img.onerror = function() { rv.reject(); };
img.src = '/images/home/dot.webp';
return rv.promise();
}
hasWebP().then(function() {
var img=document.createElement("img");
img.src="images/home/IMG_4389.webp";
img.id="picture";
var foo = document.getElementById("myFace");
foo.appendChild(img);
}, function() {
var img=document.createElement("img");
img.src="images/home/IMG_4389.png";
img.id="picture2";
var foo = document.getElementById("myFace");
foo.appendChild(img);
});
img.src="/images/home/IMG_4389.png"(note the / at the start of the src) and it appended the image to the DOM for me