如何获取网站icon有哪些可行的方法
更好的办法是通过google提供的服务来实现:
http://www.google.com/s2/favicons?domain=http://www.baidu.com
代码:
<!doctypehtml>
<html>
<head>
<metacharset="utf-8">
<styletype="text/css">
#input{
height:300px;
padding:10px5px;
line-height:20px;
width:1000px;
}
#submit{
height:30px;
text-align:center;
color:#ffffff;
line-height:30px;
width:80px;
background-color:blue;
margin-top:20px;
}
#result{
margin-top:20px;
}
#resultli{
height:40px;
line-height:40px;
float:left;
margin:10px14px;
}
</style>
</head>
<body>
<textareaid="input"placeholder="输入多个网址以空格间隔"></textarea>
<divid="submit">获取icon</div>
<ulid="result">
</ul>
<scripttype="text/javascript">
varinput=document.getElementById("input");
varsubmit=document.getElementById("submit");
varresult=document.getElementById("result");
varval;
functiontrim(str){
varwhitespace='\n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for(vari=0,len=str.length;i<len;i++){
if(whitespace.indexOf(str.charAt(i))===-1){
str=str.substring(i);
break;
}
}
for(i=str.length-1;i>=0;i--){
if(whitespace.indexOf(str.charAt(i))===-1){
str=str.substring(0,i+1);
break;
}
}
returnwhitespace.indexOf(str.charAt(0))===-1?str:'';
}
functiongetFavIconUrl(url){
varprohost;
prohost=url.match(/([^:\/?#]+:\/\/)?([^\/@:]+)/i);
prohost=prohost?prohost:[true,"http://",document.location.hostname];
//补全url
if(!prohost[1]){
prohost[1]="http://";
}
//抓取ico
return"http://www.google.com/s2/favicons?domain="+prohost[1]+prohost[2];
}
submit.onclick=function(){
val=input.value;
if(!val)alert("输入为空!");
val=val.split("");
val.forEach(function(item){
item=trim(item);
if(!item)return;
result.innerHTML+="<li>"+item+"<imgsrc='"+getFavIconUrl(item)+"'></li>";
});
};
</script>
</body>
</html>
源代码下载