Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get favicon's URL from a generic webpage in Javascript? [closed]

Tags:

javascript

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. without using an external service or library.

like image 938
xRobot Avatar asked Apr 23 '12 14:04

xRobot


People also ask

How do I find the URL of my favicon?

Right-click on the website and click the "View page info" option from the list. It will open up a dialog and click on the "Media" tab. In that tab you will see all the images including favicon.

Where can I find favicon?

A favicon is a small image that is located in the browser tab to the left of a webpage's title.

How do I create a dynamic favicon?

To change favicons, just go favicon. change("ICON URL") using the above. (credits to http://softwareas.com/dynamic-favicons for the code I based this on.)

What is favicon in Javascript?

A favicon is a small image displayed next to the page title in the browser tab.


2 Answers

For people still not getting the favicon with above codes;

  1. Most browsers support getting the favicon by sending a request (/favicon.ico) themselves, instead of in the html.

  2. Another solution is provided by Google.

    To get the favicon for a domain, use:
    https://s2.googleusercontent.com/s2/favicons?domain=www.stackoverflow.com

    To get the favicon for an URL, use:
    https://s2.googleusercontent.com/s2/favicons?domain_url=https://www.stackoverflow.com

like image 116
jerone Avatar answered Sep 29 '22 01:09

jerone


This seems to work:

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());​ 

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

like image 31
T. Zengerink Avatar answered Sep 29 '22 03:09

T. Zengerink