国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

如何獲取網站icon有哪些可行的方法

2019-11-20 14:50:07
字體:
來源:轉載
供稿:網友
獲取網站icon,常用最簡單的方法就是通過website/favicon.ico來獲取,不過由于很多網站都是在頁面里面設置favicon,所以此方法很多情況都不可用。

更好的辦法是通過google提供的服務來實現(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="輸入多個網址以空格間隔"></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];

//補全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>

源代碼下載
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邢台县| 收藏| 广宁县| 武乡县| 凤庆县| 平遥县| 巴里| 正镶白旗| 宜兴市| 瑞安市| 休宁县| 望谟县| 凤庆县| 田林县| 鹤岗市| 卢氏县| 青神县| 光山县| 广水市| 南汇区| 赣州市| 湘潭市| 吐鲁番市| 德阳市| 胶南市| 黔南| 蕲春县| 哈密市| 和龙市| 金溪县| 鄱阳县| 聊城市| 渭南市| 乌拉特中旗| 伊春市| 江达县| 通山县| 日土县| 古浪县| 土默特右旗| 富平县|