最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect 403 Errors on images in one place? - Stack Overflow

programmeradmin0浏览0评论

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

From Chrome Console

Failed to load resource: the server responded with a status of 403 (Forbidden: category denied) "Followed by URL"

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

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

From Chrome Console

Failed to load resource: the server responded with a status of 403 (Forbidden: category denied) "Followed by URL"

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

Share Improve this question edited May 3, 2013 at 15:53 asked Mar 19, 2013 at 15:41 user1637281user1637281
Add a ment  | 

3 Answers 3

Reset to default 4

I found this:

 function imgError(image){
        image.onerror = "";
        image.src = "/images/noimage.gif";
        return true;
    }

    <img src="someimage.png" onerror="imgError(this);"/>

If you use jquery you can do:

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

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

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

发布评论

评论列表(0)

  1. 暂无评论