如何獲取網(wǎng)站icon有哪些可行的方法
更新時間:2014年06月05日 17:05:47 作者:
獲取網(wǎng)站icon,常用最簡單的方法就是通過website/favicon.ico來獲取,還可以通過google提供的服務(wù)來實現(xiàn),下面有個示例
獲取網(wǎng)站icon,常用最簡單的方法就是通過website/favicon.ico來獲取,不過由于很多網(wǎng)站都是在頁面里面設(shè)置favicon,所以此方法很多情況都不可用。
更好的辦法是通過google提供的服務(wù)來實現(xiàn):
http://www.google.com/s2/favicons?domain=http://www.baidu.com
代碼:
<!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="輸入多個網(wǎng)址以空格間隔"></textarea>
<div id="submit">獲取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 = ' \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);
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];
//補(bǔ)全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 + "<img src='" + getFavIconUrl(item) + "'></li>";
});
};
</script>
</body>
</html>
源代碼下載
更好的辦法是通過google提供的服務(wù)來實現(xiàn):
http://www.google.com/s2/favicons?domain=http://www.baidu.com
代碼:
復(fù)制代碼 代碼如下:
<!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="輸入多個網(wǎng)址以空格間隔"></textarea>
<div id="submit">獲取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 = ' \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);
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];
//補(bǔ)全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 + "<img src='" + getFavIconUrl(item) + "'></li>";
});
};
</script>
</body>
</html>
源代碼下載
相關(guān)文章
兩個JavaScript中的特殊值null和undefined詳解
Null和Undefined是JavaScript中非?;A(chǔ)和重要的概念,理解它們的含義、特點和使用方式對于避免出現(xiàn)錯誤和編寫健壯的應(yīng)用程序非常重要,這篇文章主要介紹了兩個JavaScript中的特殊值null和undefined詳解,需要的朋友可以參考下2023-06-06MUI頂部選項卡的用法(tab-top-webview-main)詳解
最近用MUI做手機(jī)app應(yīng)用的時候,遇到的小bug,這里小編給大家分享MUI頂部選項卡的用法(tab-top-webview-main)詳解,感興趣的朋友一起看看吧2017-10-10解決function函數(shù)內(nèi)的循環(huán)變量
鼠標(biāo)放到指定的行上自動彈出當(dāng)前的個數(shù),從0開始,這個功能方便我們在tab切換中定位2008-10-10JS實現(xiàn)讓網(wǎng)頁背景圖片斜向移動的方法
這篇文章主要介紹了JS實現(xiàn)讓網(wǎng)頁背景圖片斜向移動的方法,涉及javascript操作背景圖片特效的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02JavaScript Typescript基礎(chǔ)使用教程
TypeScript是Microsoft(微軟)開發(fā)的一種開源編程語言,它充分利用了JavaScript原有的對象模型,并在此基礎(chǔ)上進(jìn)行了擴(kuò)充,TypeScript設(shè)計目標(biāo)是開發(fā)大型應(yīng)用,它可以編譯成純JavaScript,編譯出來的JavaScript可以運行在任何一種JS運行環(huán)境中2022-12-12Javascript的數(shù)組與字典用法與遍歷對象的屬性技巧
Javascript 的數(shù)組Array,既是一個數(shù)組,也是一個字典(Dictionary)。先舉例看看數(shù)組的用法2012-11-11