Detect when no image is found in a collection

I have a collection which contains images that are gathered through a URL (the images are hosted on my own server). I need to check whether each entry contains an image or if none exist for this entry.

For every entry, there is a link, however this link may not always link to an image and may return a 404.

How can check whether the URL in my collection contains an image.

Thanks

The context of this snippet depends on your layout and what your images are in (dynamic page or repeater, i.e.), so it could be:

if (!itemData.image) { } //for repeaters

or

if (!$w('#dataset').getCurrentItem().image) { } //for dynamic pages

where .image is replaced with your collection’s field key.

Hi David, thanks for the answer.

However that doesn’t quite answer my question.

In the snippet you provide above,

if (!itemData.image)

will always be false since the image field is a URL, and that every entry has such an URL.

The question was regarding how to detect if the URL links to an image or if the URL returns a 404. I still haven’t been able to find a property that could help me with this.

Any help is appreciated. Thanks!

EDIT: I had some ideas, like adding an alt text to the image and check if it is rendered or not, but nothing has been successful so far.

The issue involves way more than just a property . The only way to know if a URL points to an actual image is to perform the actual link and see if something is returned. This would have overhead and depending on usage might or might not be suitable for a live site.

Thanks for your reply Yisrael. This feature is quite important to my website. I’ll try it and if it is not suitable, will discuss it with my client. Could you let me know how one can see what is returned when performing a link in Wix code?
Thanks

I did a quick search and you might be able to learn from the validate-image-url project. You can implement your own in the backend code.