如何从Javascript中的通用网页获取favicon的URL? [英] How to get favicon's URL from a generic webpage in Javascript?

查看:713
本文介绍了如何从Javascript中的通用网页获取favicon的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法从通用网页获取favicon的URL,因为favicon并不总是在基本网址。

I need a way to get the favicon's URL from a generic webpage considering that the favicon is not always at the base url.

P.s。不使用外部服务或库。

P.s. without using an external service or library.

推荐答案

这似乎有效:

var getFavicon = function(){
    var favicon = undefined;
    var nodeList = document.getElementsByTagName("link");
    for (var i = 0; i < nodeList.length; i++)
    {
        if((nodeList[i].getAttribute("rel") == "icon")||(nodeList[i].getAttribute("rel") == "shortcut icon"))
        {
            favicon = nodeList[i].getAttribute("href");
        }
    }
    return favicon;        
}

alert(getFavicon());​

或者查看 http://jsfiddle.net/PBpgY/3/ 获取在线示例。

Or have a look at http://jsfiddle.net/PBpgY/3/ for an online example.

这篇关于如何从Javascript中的通用网页获取favicon的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆