How can I retrieve the favicon of a website with XSLT or JSP?

Viewed 58307

I want to list featured websites on my website and I thought it would be cool to honor and use their favicon. How do I get it from the domain for an arbitrary URL in either JSP or XSLT? I can fire off PHP or javascript, but XSLT is the preferred methodology.

6 Answers

To get the favicon of a website, you need to load the index HTML of each featured website and check for either of the following:

HTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="http://example.com/image.ico">
<link rel="icon" type="image/png" href="http://example.com/image.png">
<link rel="icon" type="image/gif" href="http://example.com/image.gif">

XHTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="/somepath/image.ico" />
<link rel="icon" type="image/png" href="/somepath/image.png" />
<link rel="icon" type="image/gif" href="/somepath/image.gif" />

Internet Explorer may use a slightly different format:

<link rel="SHORTCUT ICON" href="http://www.example.com/myicon.ico" />

Also note that since most web browsers do not require the HTML link to retrieve a favicon, you should also check for favicon.ico in the website's document root, if none of the above link references are found.

With PHP, it is easy to get the HTML contents of a web page by using file_get_contents($url):

$url = 'http://www.exmaple.com';
$output = file_get_contents($url);
  • Using IE, bookmark the site

  • Drag the shortcut from your bookmarks menu onto your desktop

  • Open the resulting .URL using a (real) text editor

  • There will be a line in the file for IconFile, which will point to the favicon file on the web server

  • Browse to the file... viola!

Related