Hello, I write on Vanilla Js. The code returns a link to the image from the server, and then I put it as a background-image url. But how to do so, if the server returned 404 instead of an empty picture, put your own with an error?

  • Did not understand the question - Yuri
  • learn.javascript.ru/onload-onerror read this article, maybe it will help :) -
  • The article above will not help. But the hack of this answer can help. Only it is necessary to rewrite it for vanilla. In short, you create a picture in memory, check whether it has loaded, and only then place it in the background-image . If not loaded, then put another image with an error. But in general, your approach is fundamentally wrong. Content can not be background-image in the background-image . And if you don’t have content there, but you shove design, then the only question is why you cannot guarantee the availability of a picture that is part of the design. - neluzhin

1 answer 1

If ssilka_servera.jpg not transparent, you can use several backgrounds:

 background-image: url('ssilka_servera.jpg'), url('default.jpg'); 

default.jpg will be visible only if ssilka_servera.jpg not available