开发者

How do I validate favicons using Javascript?

In a previous post on SO I asked how to display favicons with a generic option for favicons that do not exist.

<img src="example.com/favicon.ico"; alt=""/>

This code was posted:

var img = document.getElementsByTagName('img')[0],
    favicon = new Image;

favicon.src = 'http://example.com/favicon.ico'

favicon.onerror = function() 
  {
  img.src = 'http://url-on-local-server.com/favicon.ico';
  }

However I do not understand the need for "new Image", why can't I just use 开发者_JAVA百科the existing image like this?

var img = document.getElementsByTagName('img')[0],
img.src = 'http://example.com/favicon.ico'
img.onerror = function() 
  {
  img.src = 'http://url-on-local-server.com/favicon.ico';
  }


Found this link, wish I would have found this an hour ago

<img src="image.gif" onerror="src_to_generic()" />

at

w3 schools

Just call a function on the onerror that resets the src to a generic image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜