Adding favicons with generic option

徘徊边缘 提交于 2019-12-25 03:47:16

问题


I'd like to add favicons to my site using favicon.ico.

however I want to load a generic icon if the site does not provide one.

How can I test for the presense of a favicon and if one is not present manipulate the DOM to point to a generic favicon on the server.


回答1:


You could use some JavaScript. Adapt to suit...

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

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

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



回答2:


Found this link, wish I would have found this sooner.

<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.



来源:https://stackoverflow.com/questions/7034333/adding-favicons-with-generic-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!