获取网站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的更多相关文章

  1. 获取网站图标Icon

    通常情况下,做网站的都会给自己的网站添加一个Icon,浏览器上一长排的标签页,用Icon来区分就显得更加醒目.现在想找一个没有Icon的网站并不好找,可见没有Icon的网站是多么的业余啊." ...

  2. 再谈获取网站图标Icon

    上一篇文章讨论了一下获取网站图标方法,是通过从根目录直接获取和html解析结合的方式来获取的,并给出了相应的代码示例.这一篇来讨论一个更现成的方法,这个方法是从360导航的页面发现的,在导航页面中点击 ...

  3. PHP获取网站图标(favicon.ico)文件

    有的网站源码中加入了这几行代码: <link rel="shortcut icon" href="/favicon.ico" type="ima ...

  4. 使用PHP获取网站Favicon的方法

    使用PHP获取网站Favicon的方法 Jan022014 作者:Jerry Bendy   发布:2014-01-02 23:18   分类:PHP   阅读:4,357 views   20条评论 ...

  5. 曲线救国:IIS7集成模式下如何获取网站的URL

    如果我们在Global中的Application_Start事件中访问HttpContext.Current.Request对象,如: protected void Application_Start ...

  6. js获取网站根目录

    //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址         function getRootPath(){        var strFullPath=window ...

  7. php获取网站根目录

    php获取网站根目录方法一:<?phpdefine("WWWROOT",str_ireplace(str_replace("/","\\&quo ...

  8. 【ASP.NET】获取网站目录的方法

         获取网站物理路径: HttpRuntime.AppDomainAppPath 获取网站虚拟路径: HttpRuntime.AppDomainAppVirtualPath

  9. .net 获取网站根目录总结

    一.获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables["PATH_INFO"]) //页面详细路 Server.MapP ...

随机推荐

  1. HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)

    Problem Description As is known to all, Sempr(Liangjing Wang) had solved more than 1400 problems on ...

  2. org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.

    No action config found for the specified url url路径下找不到action,原因是stuts-config.xml文件配置错误. demo的项目文件如下: ...

  3. Android学习之电话拨号器

    本人自己是做android驱动的,也会接触到系统层.上层的应用,所以在闲暇的时候也就开始了学习android应用的路程,在这里把这些东西记下来,希望自己能坚持下去,也好以后复习用. 今天先实现一个简单 ...

  4. WebView中的视频全屏的相关操作

    近期工作中,基本一直在用WebView,今天就把它整理下: WebView 顾名思义,就是放一个网页,一个看起来十分简单,可是用起来不是那么简单的控件. 首先你肯定要定义,初始化一个webview,事 ...

  5. [ES6] ... spread operator

    var parts = ['shoulders', 'knees']; var lyrics = ['head', ...parts, 'and', 'toes']; // ["head&q ...

  6. mysql将一个库中表的某几个字段插入到另一个库中的表

    insert into dbname1.tablename1(filed1,filed2,filed3) select filed1,filed2,filed3from dbname2.tablena ...

  7. fork安全的gettid高效实现

    进程有id,可以通过getpid()获得,线程也有id,但是glibc没有提供封装.需要自己发出系统调用.在关键路径,系统调用还是对性能有影响的.因此我们可以想到类似glibc对getpid做的cac ...

  8. MVP模式 详解 案例

    介绍 MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 实际上关于该布局文件中的数据绑定的操作,事件处理的代码都在Activity中,造 ...

  9. MyEclipse设置默认的文档注释

  10. 你以为你了解最常用的string.substring()的几个常见问题吗?

    ---恢复内容开始--- 前言: 1.项目中我们难免会用到各种对字符串的处理方法,可是你曾知道substring()这个用法别有洞天?你考虑过一下几个情况吗? 使用Substring()时的正确写法: ...