1

Is there way to check if link to image is working before adding src attribute to image? What I have tried:

  • First idea was to make ajax call to image src but I cannot be sure that CORS pocily will allow it.
  • Adding onError callback into my component. No luck because I use SSR and it causes problems

My use case: I'm building text editor and user can insert link to image. I want to know if link is ok before inserting it into my model.

Kostya Tresko
  • 754
  • 1
  • 5
  • 24

1 Answers1

1

The other answers/question aren't specifically about cross-domain requests. If you only have to support modern browsers you can use an opaque fetch request which works cross domain and is pretty easy to use.

fetch('http://example.com/your-image-url', { mode: 'no-cors' }).then(() => {
 if (response.ok) {
   // the image exists
 }
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504