I have a url. How can I check if that url works and displays the image and if the link doesn’t work or is broken I want to check if a different url works the if this one doesn’t work to just log all urls don’t work. How can I achieve this. Thanks in advance.
//I have a url like:
const myUrl = 'https://img.youtube.com/vi/Q5twKgxO8xg/maxresdefault.jpg'
//How can I check if th url work and if not try a different url
if (myUrl Works){
console.log('url works')
} else {
//check if this url works: 'https://img.youtube.com/vi/Q5twKgxO8xg/hqdefault.jpg'
if (newUrl works) {
console.log('new url works')
} else {
console.log('all urls broken')
}
}
3
Answers
You can use fetch API to check whether the image is available or not:
You can use jQuery AJAX to check your image:
You can use the
onerror
global attribute on aimg
element.