如何获取网站icon
获取网站icon,常用最简单的方法就是通过website/favicon.ico来获取,不过由于很多网站都是在页面里面设置favicon(<link rel="shortcut icon" href="http://example.com/favicon.ico" />),所以此方法很多情况都不可用。
更好的办法是通过google提供的服务来实现: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>
源代码下载:http://files.cnblogs.com/shinnyChen/getIcon.rar
后记:
对于国内的网站,也可以使用360的服务:
http://cdn.website.h.qhimg.com/index.php?domain=www.baidu.com
如何获取网站icon的更多相关文章
- 获取网站图标Icon
通常情况下,做网站的都会给自己的网站添加一个Icon,浏览器上一长排的标签页,用Icon来区分就显得更加醒目.现在想找一个没有Icon的网站并不好找,可见没有Icon的网站是多么的业余啊." ...
- 再谈获取网站图标Icon
上一篇文章讨论了一下获取网站图标方法,是通过从根目录直接获取和html解析结合的方式来获取的,并给出了相应的代码示例.这一篇来讨论一个更现成的方法,这个方法是从360导航的页面发现的,在导航页面中点击 ...
- PHP获取网站图标(favicon.ico)文件
有的网站源码中加入了这几行代码: <link rel="shortcut icon" href="/favicon.ico" type="ima ...
- 使用PHP获取网站Favicon的方法
使用PHP获取网站Favicon的方法 Jan022014 作者:Jerry Bendy 发布:2014-01-02 23:18 分类:PHP 阅读:4,357 views 20条评论 ...
- 曲线救国:IIS7集成模式下如何获取网站的URL
如果我们在Global中的Application_Start事件中访问HttpContext.Current.Request对象,如: protected void Application_Start ...
- js获取网站根目录
//js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ var strFullPath=window ...
- php获取网站根目录
php获取网站根目录方法一:<?phpdefine("WWWROOT",str_ireplace(str_replace("/","\\&quo ...
- 【ASP.NET】获取网站目录的方法
获取网站物理路径: HttpRuntime.AppDomainAppPath 获取网站虚拟路径: HttpRuntime.AppDomainAppVirtualPath
- .net 获取网站根目录总结
一.获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables["PATH_INFO"]) //页面详细路 Server.MapP ...
随机推荐
- POJ 1700 坐船过河问题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82974#problem/E 解题思路:当n>=4,假设n个人单独过河所需 ...
- Eclipse无法设置NDK路径的解决方法
如果在Eclipse中设置NDK路径的时候遇到下面的情况(尼玛,我的NDK设置呢?): 可以参考百度经验解决:http://jingyan.baidu.com/article/4d58d5413000 ...
- flash 中无法导出swf文件的解决方法
近一个星期,我的flash cs6一直导不出swf文件,郁闷了好长时间,今天终于在网上找到了解决办法:总结了一下,如下: 一.是把文字打散,变成形状. 二.是把汉字的字体设成fla ...
- 【转】ASP.NET MVC 数据验证及相关内容
原文地址:http://www.jb51.net/article/56713.htm 一.数据验证 数据验证的步骤在模型类中添加与验证相关的特性标记在客户端导入与验证相关的js文件和css文件使用与验 ...
- asp.net using library ClosedXML to export excel
Reference: http://closedxml.codeplex.com/ 1. First add refenrence ClosedXML.dll and DocumentFormat. ...
- Android 基础组件
基础组件 所有的控件都可以在java代码中创建出来,并且大部分的属性都对应set和get方法,比如 View view = new View(Context context) context是上下文 ...
- UVa 11063 - B2-Sequence
题目:给你一组数据{ b1,b2....,bk }中,推断是否随意两个数字的和都不同. 分析:数论.计算出全部结果,排序推断相邻结果是否同样就可以. 说明:注意数据的合法性检查. #include & ...
- Tomcat: IllegalStateException: No output folder --reference
Today, I started to create a couple of JSP pages for the server-side part of my MSc thesis project i ...
- machine learning in action , part 1
We should think in below four questions: the decription of machine learning key tasks in machine lea ...
- ASP.NET-FineUI开发实践-7
下拉显示grid列表.其实很简单,但是试了很多方法,水平有限,主要是都不好使,还是简单的好使了,分享下. 先是看了看网上的,是直接写个了extjs控件类(我也不懂),然后直接用就行了,要写成FineU ...