如何获取网站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 ...
随机推荐
- visual studio 调试grunt
原文地址: https://yoavniran.wordpress.com/2015/06/25/debug-grunt-tasks-in-visual-studio-2015/
- Demon_游戏登录界面(具备账号密码输入功能)
using UnityEngine; using System.Collections; using UnityEngine.UI;// public class LoginButton : Mono ...
- ffmpeg错误隐藏框架分析
本文主要是分析ffmpeg的错误隐藏框架,故解码流程此处不会特地进行讨论,网上其他地方其实也有不少介绍相关流程的了,但发现基本没有介绍错误隐藏流程的,故本文希望能填补这个空白. 我们直接从decode ...
- 网络编程TCP总结及实践-C语言
网络变成首先要注意IP和port的转换,如今电脑基本上是主机字节序,存储依照小端方式,而在网络中传输统一使用大端方式,所以网络变成首先要注意字节序的转换. 一个经常使用的ip转换程序的实现: #inc ...
- [Javascript] Intro to Recursion - Refactoring to a Pure Function
Previous post: http://www.cnblogs.com/Answer1215/p/4990418.html let input, config, tasks; input = [' ...
- 不相交集python实现
1.不相交集是解决等价关系的一种数据结构,执行合并和查找的速度都很快,M次执行合并和查找的执行时间为(M*logN). 在一个集合中.对于每一对元素(a,b),a,b∈S,对于关系R假设满足以下三个条 ...
- honeywell D6110开发的一个工厂仓库追溯识别
近日.接触并开发了一个用honeywell D6110 二维扫描PDA的项目,应用也比較简单. 就是货品物料编码.通过中间码相应,然后中间码再依照不同OEM品牌须要生成各种商品条码并带有流水号. 要求 ...
- JAVA实现AES和MD5加密
package test; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; ...
- Android时间戳转换为标准Datetime(yyyy-MM-dd hh:mm:ss)格式
下列函数为实现过程,已经测试通过. /// <summary> /// Android时间戳转换为标准Datetime /// </summary> /// <param ...
- PHP对表单提交特殊字符的过滤和处理
PHP关于表单提交特殊字符的处理方法做个汇总,主要涉及htmlspecialchars/addslashes/stripslashes/strip_tags/mysql_real_escape_str ...