Using Javascript I can replace image by changing src
parameter, such as
document.getElementById('image-id').src = 'new-image.png';
If I need change image dynamically when and only the new image is available and valid, how I need to adjust the above code to perform replacement only if browser is able to get the image?
Other words, if HTTP request for new-image.png
ends with error (403, 404, 500, ...), or if the file is not a valid image, I want to keep the original image and do not execute the code, because otherwise browser shows no image.