4

[JavaScript] Load Favicon Dynamically

 2 years ago
source link: http://siongui.github.io/2012/10/02/javascript-load-favicon-dynamically/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[JavaScript] Load Favicon Dynamically

Updated: February 23, 2015

This post is an extension of reference [1], which discusses how to load external JavaScript or CSS dynamically. The same idea can be applied to load favicon dynamically. Give the URL or path of favicon to the following function, and then favicon will be loaded dynamically:

function LoadFavicon(url) {
  var ext = document.createElement('link');
  ext.setAttribute("rel", "shortcut icon");
  ext.setAttribute("href", url);
  document.getElementsByTagName("head")[0].appendChild(ext);
}

Example Usage of LoadFavicon Function

Assume that the path of favicon is located at /favicon.ico under the domain of the website. You can call:

LoadFavicon('/favicon.ico');

to load favicon dynamically. Even if your favicon is located at a different domain, you can still pass the URL of favicon at the different domain to above function and it also works!

Enjoy!


Reference:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK