Fetch and set image from URL
by Imri Paloja
JavaScript
const myImage = document.querySelector('img');
const myRequest = new Request('https://www.w3schools.com/html/pic_trulli.jpg');
window
.fetch(myRequest)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.blob();
})
.then((response) => {
myImage.src = URL.createObjectURL(response);
});