views:

428

answers:

3

Hello !

I am wondering if it is possible to get a website's favicon by an URL with JavaScript.

For example I have an URL http://www.bbc.co.uk/ and I would like to get path to favicon described in <link rel="icon" .../> meta tag - http://www.bbc.co.uk/favicon.ico.

I have many URLs so that should not load every page and search for link tag I think.

Any ideas ?

+1  A: 

Suddenly I found something called Google Shared Stuff that returns image with website's favicon by hostname:

http://www.google.com/s2/favicons?domain=www.domain.com

But fot BBC site it returns favicon a bit small. Compare:

http://www.google.com/s2/favicons?domain=www.bbc.co.uk
http://www.bbc.co.uk/favicon.ico

hsz
The ICO format can have multiple images of multiple sizes, Google's S2 just gives you the 16x16 one (the most used one).
Maushu
Google Shared Stuff has since been taken offline.
Ken Liu
A: 

You could use YQL for that

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D"http://bbc.co.uk/"and%20xpath%3D"/html/head/link[@rel%3D'icon']%20|%20/html/head/link[@rel%3D'ICON']%20|%20/html/head/link[@rel%3D'shortcut%20icon']%20|%20/html/head/link[@rel%3D'SHORTCUT%20ICON']"&format=json&callback=grab

This query used by Display Feed Favicons Greasemonkey script.

You can write queries in YQL console, but it requires to login (btw, using queries don't):

http://developer.yahoo.com/yql/console/#h=select%20*%20from%20html%20where%20url%3D%22http%3A//bbc.co.uk/%22and%20xpath%3D%22/html/head/link%5B@rel%3D%27icon%27%5D%20%7C%20/html/head/link%5B@rel%3D%27ICON%27%5D%20%7C%20/html/head/link%5B@rel%3D%27shortcut%20icon%27%5D%20%7C%20/html/head/link%5B@rel%3D%27SHORTCUT%20ICON%27%5D%22

It is better than http://www.google.com/s2/favicons?domain=www.domain.com , in case favicon exists, but doesn't located in domain.com/favicon.ico

NV
It requires to login.
hsz
I've updated answer.
NV
+1  A: 

Here are 3 options , I tested over 100 urls and got different results which each option.

<img height="16" width="16" src='http://getfavicon.appspot.com/http://www.edocuments.co.uk' />

<img height="16" width="16" src='http://www.getfavicon.org/?url=www.edocuments.co.uk' />

<img height="16" width="16" src='http://www.google.com/s2/favicons?domain=www.edocuments.co.uk' />
Blowsie