获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon,所以此方法很多情况都不可用。 更好的办法是通过google提供的服务来实现:http://www.google.com/s2/favicons?domain=http://www.baidu.com 代码: 复制代码 代码如下: <BR>#input { <BR>height: 300px; <BR>padding: 10px 5px; <BR>line-height: 20px; <BR>width: 1000px; <BR>} <BR>#submit { <BR>height: 30px; <BR>text-align: center; <BR>color: #ffffff; <BR>line-height: 30px; <BR>width: 80px; <BR>background-color: blue; <BR>margin-top: 20px; <BR>} <BR>#result { <BR>margin-top: 20px; <BR>} <BR>#result li { <BR>height: 40px; <BR>line-height: 40px; <BR>float: left; <BR>margin: 10px 14px; <BR>} <BR> 获取icon <BR>var input = document.getElementById("input"); <BR>var submit = document.getElementById("submit"); <BR>var result = document.getElementById("result"); <BR>var val; <br><br>function trim(str) { <BR>var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000'; <BR>for (var i = 0, len = str.length; i < len; i++) { <BR>if (whitespace.indexOf(str.charAt(i)) === -1) { <BR>str = str.substring(i); <BR>break; <BR>} <BR>} <BR>for (i = str.length - 1; i >= 0; i--) { <BR>if (whitespace.indexOf(str.charAt(i)) === -1) { <BR>str = str.substring(0, i + 1); <BR>break; <BR>} <BR>} <BR>return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; <BR>} <br><br>function getFavIconUrl(url) { <BR>var prohost; <BR>prohost = url.match(/([^:\/?#]+:\/\/)?([^\/@:]+)/i); <BR>prohost = prohost ? prohost : [true, "http://", document.location.hostname]; <br><br>//补全url <BR>if (!prohost[1]) { <BR>prohost[1] = "http://"; <BR>} <BR>//抓取ico <BR>return "http://www.google.com/s2/favicons?domain=" + prohost[1] + prohost[2]; <BR>} <BR>submit.onclick = function() { <BR>val = input.value; <BR>if (!val) alert("输入为空!"); <BR>val = val.split(" "); <BR>val.forEach(function(item) { <BR>item = trim(item); <BR>if (!item) return; <BR>result.innerHTML += "<li>" + item + "<img src='" + getFavIconUrl(item) + "' alt="如何获取网站icon有哪些可行的方法_javascript技巧" >"; <BR>}); <BR>}; <BR> 源代码下载