繁体   English   中英

如何在javascript变量中存储任何网址的favicon.ico?

[英]How to store the favicon.ico of any url in a javascript variable?

好的,我知道如何获取并显示任何网址的网站图标。

icon[x].setAttribute("src","http://"+ curl + "/favicon.ico")

“ curl”是特定网址的域部分

但是在为任何URL设置网站图标之前,我要检查它是否实际上已获取任何网站图标。 因为对于某些站点,没有获取网站图标,并且在我的页面上没有显示该网址的图标。 所以基本上我想先在变量中获取收藏夹图标,然后检查变量中是否有任何图标。 如果没有,它将放置默认图像。 我只想知道如何获取图标将其存储在变量中,然后检查是否存在任何图像。

这是我的代码...请检查一次

编辑:

如果我正确理解您的问题,我不确定100%。

var icon = '';
for(var a = 0; a < document.getElementsByTagName('link').length;a++) { 
 if(document.getElementsByTagName('link')[a].getAttribute('rel') == 'shortcut icon') {
  // favicon found
  icon = document.getElementsByTagName('link')[a].getAttribute('href');
 }
}
// if no favicon is found, 'icon' is an empty string
// and a new DOM element ("link") needs to be added to show the icon
if(icon == '') {
 var node = document.createElement("LINK"); 
 node.setAttribure('rel', 'shortcut icon');
 node.setAttribute('href','http://link.to/favicon.ico');
 document.getElementsByTagName('head')[0].appendChild(node);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM