When submitting a form, I want to make sure a field is a valid image URL.
I could make an AJAX endpoint on my server which CURLs the URL and parses the output with an image library, but that feels a bit of overkill.
Could I get away with making an <img> element then synchronously check the response somehow?
You can make an
<img>element and handle itsonerrorandonloadevents.If the
loadevent fires, it’s a valid image; if theerrorevent fires, it isn’t.This even works across domains.