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.
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.
A favicon is a small image that is located in the browser tab to the left of a webpage's title.
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.)
A favicon is a small image displayed next to the page title in the browser tab.
For people still not getting the favicon with above codes;
Most browsers support getting the favicon by sending a request (/favicon.ico
) themselves, instead of in the html.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With