La forma más fácil de obtener el ícono del sitio web es obtenerlo a través del sitio web/favicon.ico. Sin embargo, dado que muchos sitios web configuran Favicon en la página, este método no está disponible en muchas situaciones.
Una mejor manera es lograr esto a través de los servicios proporcionados por Google:
http://www.google.com/s2/favicons?domain=http://www.baidu.com
Código:
La copia del código es la siguiente:
<! Doctype html>
<html>
<Evista>
<meta charset = "utf-8">
<style type = "text/css">
#aporte {
Altura: 300px;
relleno: 10px 5px;
Línea de altura: 20px;
Ancho: 1000px;
}
#entregar {
Altura: 30px;
Text-Align: Center;
Color: #ffffff;
Línea de altura: 30px;
Ancho: 80px;
color de fondo: azul;
margen-top: 20px;
}
#resultado {
margen-top: 20px;
}
#result li {
Altura: 40px;
Línea de altura: 40px;
flotante: izquierda;
margen: 10px 14px;
}
</style>
</ablo>
<Body>
<Textarea id = "input" PlaceHolder = "Ingrese múltiples URL con intervalos espaciales"> </extAREA>
<div id = "enviar"> obtener icono </div>
<ul id = "resultado">
</ul>
<script type = "text/javaScript">
var input = document.getElementById ("entrada");
var envíe = document.getElementById ("enviar");
resultado var = document.getElementById ("resultado");
var val;
función trim (str) {
var whitespace = '/n/r/t/f/x0b/xa0/u2000/u2001/u2002/u2003/u2004/u2005/u2006/u2007/u2008/u2009/u200a/u200b/u2028/u2029/u3000';;
para (var i = 0, len = str.length; i <len; i ++) {
if (whitespace.indexof (str.charat (i)) === -1) {
str = str.substring (i);
romper;
}
}
para (i = str.length-1; i> = 0; i--) {
if (whitespace.indexof (str.charat (i)) === -1) {
str = str.substring (0, i + 1);
romper;
}
}
return whitespace.indexof (str.charat (0)) === -1? str: '';
}
función getFaviconurl (URL) {
Var prohost;
prohost = url.match (/([^: //?#]+: ////)? ([^//@:]+)/i);
Prohost = Prohost? prohost: [true, "http: //", document.location.hostname];
// Completa la URL
if (! Prohost [1]) {
prohost [1] = "http: //";
}
// gatea ICO
return "http://www.google.com/s2/favicons?domain=" + prohost [1] + prohost [2];
}
enviar.Onclick = function () {
val = input.Value;
if (! val) alerta ("La entrada está vacía!");
val = val.split ("");
val.ForEach (función (elemento) {
elemento = trim (elemento);
if (! item) return;
resultado.innerhtml + = "<li>" + item + "<img src = '" + getFaviconurl (item) + "'> </li>";
});
};
</script>
</body>
</html>
Descarga del código fuente