I am attempting to load a Youtube thumbnail using a URL. If the image from the URL does not exist or throws an error, I want to display a placeholder.
To achieve this functionality, I have written the following JavaScript
code:
const yt = document.getElementById('YoutubeThumbnail'); //img tag of my requested image
yt.addEventListener('error', (){
yt.setAttribute('src', 'placeholder');
});
This implementation works as expected, however, there is an issue where sometimes the favicon from the requested site also triggers an error. Just for reference, maxresdefault.jpg
is the specific image I need to retrieve.
https://i.sstatic.net/H2JjJ.png
Since I am handling all errors in my code, it invokes the placeholder image when the favicon generates an error even if the requested image does not encounter any issues.
Is there a way to only handle errors related to my maxresdefault.jpg
and ignore errors associated with the favicon?
Thank you for your help!