Webサイトのアイコンを取得する最も簡単な方法は、Webサイト/favicon.icoを通じてそれを取得することです。ただし、多くのWebサイトがページにFaviconを設定するため、この方法は多くの状況では利用できません。
より良い方法は、Googleが提供するサービスを通じてこれを達成することです:
http://www.google.com/s2/favicons?domain=http://www.baidu.com
コード:
コードコピーは次のとおりです。
<!doctype html>
<html>
<head>
<メタcharset = "utf-8">
<style type = "text/css">
#input {
高さ:300px;
パディング:10px 5px;
ラインハイト:20px;
幅:1000px;
}
#提出する {
高さ:30px;
テキストアライグ:センター;
色:#ffffff;
Line-Height:30px;
幅:80px;
背景色:青;
マージントップ:20px;
}
#結果 {
マージントップ:20px;
}
#ResultLi {
高さ:40px;
Line-Height:40px;
フロート:左;
マージン:10px 14px;
}
</style>
</head>
<body>
<textarea id = "input" placeholder = "スペース間隔で複数のURLを入力"> </textarea>
<div id = "submit"> getアイコン</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;
関数トリム(str){
var whitespace = '/n/r/t/f/x0b/xa0/u2000/u2001/u2002/u2003/u2004/u2005/u2006/u2007/u2008/u2009/u200a/u200b/u2028/u2029/u3000';
for(var i = 0、len = str.length; i <len; i ++){
if(whitespace.indexof(str.charat(i))=== -1){
str = str.substring(i);
壊す;
}
}
for(i = str.length-1; i> = 0; i-){
if(whitespace.indexof(str.charat(i))=== -1){
str = str.substring(0、i + 1);
壊す;
}
}
whitespace.indexof(str.charat(0))=== -1を返しますか? str: '';
}
関数getFaviconurl(url){
var Prohost;
prohost = url.match(/([^://?#]+:///)?([^//@:]+)/i);
prohost = prohost? Prohost:[true、 "http://"、document.location.hostname];
// URLを完了します
if(!prohost [1]){
prohost [1] = "http://";
}
// ICOをクロールします
"http://www.google.com/s2/favicons?domain=" + prohost [1] + prohost [2];
}
submit.onclick = function(){
val = input.value;
if(!val)alert( "input is empty!");
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>
ソースコードのダウンロード