Cara termudah untuk mendapatkan ikon situs web adalah dengan mendapatkannya melalui situs web/favicon.ico. Namun, karena banyak situs web menyiapkan Favicon di halaman, metode ini tidak tersedia dalam banyak situasi.
Cara yang lebih baik adalah mencapai ini melalui layanan yang disediakan oleh Google:
http://www.google.com/s2/favicons?domain=http://www.baidu.com
Kode:
Salinan kode adalah sebagai berikut:
<! Doctype html>
<Html>
<head>
<meta charset = "UTF-8">
<type style = "text/css">
#input {
Tinggi: 300px;
padding: 10px 5px;
Line-Height: 20px;
Lebar: 1000px;
}
#kirim {
Tinggi: 30px;
Teks-Align: tengah;
Warna: #FFFFFF;
Line-Height: 30px;
Lebar: 80px;
Latar Belakang: Biru;
margin-top: 20px;
}
#hasil {
margin-top: 20px;
}
#Result li {
Tinggi: 40px;
Line-Height: 40px;
float: kiri;
margin: 10px 14px;
}
</tyle>
</head>
<body>
<textarea id = "input" placeholder = "Masukkan beberapa URL dengan interval ruang"> </pextarea>
<Div ID = "Kirim"> Dapatkan Ikon </Div>
<ul id = "hasil">
</ul>
<type skrip = "Teks/JavaScript">
var input = document.geteLementById ("input");
var submit = document.geteLementById ("kirim");
var result = document.geteLementById ("result");
var val;
function trim (str) {
var whitespace = '/n/r/t/f/x0b/xa0/u2000/u2001/u2002/u2003/u2004/u2005/u2006/u2007/u2008/u2009/u200a/u200b/u2028/u2029/u2009';
untuk (var i = 0, len = str.length; i <len; i ++) {
if (whitespace.indexof (str.charat (i)) === -1) {
str = str.substring (i);
merusak;
}
}
untuk (i = str.length-1; i> = 0; i--) {
if (whitespace.indexof (str.charat (i)) === -1) {
str = str.substring (0, i + 1);
merusak;
}
}
return whitespace.indexof (str.charat (0)) === -1? str: '';
}
fungsi getFaviconUrl (url) {
var pruhost;
prohost = url.match (/([^: //?#]+: ////)? ([^//@:]+)/i);
Pruhost = Pruhost? Prohost: [true, "http: //", document.location.hostname];
// Lengkapi URL
if (! pruhost [1]) {
Pruhost [1] = "http: //";
}
// Crawl ICO
kembalikan "http://www.google.com/s2/favicons?domain=" + pruhost [1] + prohost [2];
}
kirim.onClick = function () {
val = input.value;
if (! val) waspada ("Input kosong!");
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>
Unduh kode sumber