What are some possible ways to get the website icon

  • 2020-03-30 03:14:25
  • OfStack

The easiest way to get a website icon is through website/favicon.ico, but this method is not available in many cases because many websites set the favicon in the page.

A better way to do this is through Google's services:
(link: http://www.google.com/s2/favicons? domain=http://www.baidu.com).

Code:
 
<!doctype html> 
<html> 

<head> 
<meta charset="utf-8"> 
<style type="text/css"> 
#input { 
height: 300px; 
padding: 10px 5px; 
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; 
} 
#result li { 
height: 40px; 
line-height: 40px; 
float: left; 
margin: 10px 14px; 
} 
</style> 
</head> 

<body> 
<textarea id="input" placeholder=" Enter multiple urls separated by Spaces "></textarea> 
<div id="submit"> To obtain icon</div> 
<ul id="result"> 

</ul> 

<script type="text/javascript"> 
var input = document.getElementById("input"); 
var submit = document.getElementById("submit"); 
var result = document.getElementById("result"); 
var val; 

function trim(str) { 
var whitespace = ' nrtfx0bxa0u2000u2001u2002u2003u2004u2005u2006u2007u2008u2009u200au200bu2028u2029u3000'; 
for (var i = 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; 
} 
} 
return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; 
} 

function getFavIconUrl(url) { 
var prohost; 
prohost = url.match(/([^:/?#]+://)?([^/@:]+)/i); 
prohost = prohost ? prohost : [true, "http://", document.location.hostname]; 

//Completion url
if (!prohost[1]) { 
prohost[1] = "http://"; 
} 
//Grab ico
return "http://www.google.com/s2/favicons?domain=" + prohost[1] + prohost[2]; 
} 
submit.onclick = function() { 
val = input.value; 
if (!val) alert(" Enter null! "); 
val = val.split(" "); 
val.forEach(function(item) { 
item = trim(item); 
if (!item) return; 
result.innerHTML += "<li>" + item + "<img src='" + getFavIconUrl(item) + "'></li>"; 
}); 
}; 
</script> 
</body> 
</html> 

(link: http://xiazai.jb51.net/201406/yuanma/getIcon (jb51.net). Rar)

Related articles: