How to detect 403 errors on images in one place?

This error occurs when my site is used inside an organization that blocks certain domains - i.e. social sites etc. These are just links to images in these domains using <img src = "">

In the Chrome console

Failed to load the resource: the server responded with the status 403 (Forbidden: category is prohibited) "URL followed"

How can I detect this error pragmatically and display the default image instead?

+4
source share
3 answers

I found this:

  function imgError(image){ image.onerror = ""; image.src = "/images/noimage.gif"; return true; } <img src="someimage.png" onerror="imgError(this);"/> 
+2
source

If you use jquery you can do:

 $("img").error(function(){ $(this).attr("src", "default-image.png"); }); 
+2
source

You can use the onerror attribute to execute a JavaScript function to change the src attribute.

http://www.w3schools.com/jsref/event_img_onerror.asp

0
source

All Articles