Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing website favicon dynamically

I have a web application that's branded according to the user that's currently logged in. I'd like to change the favicon of the page to be the logo of the private label, but I'm unable to find any code or any examples of how to do this. Has anybody successfully done this before?

I'm picturing having a dozen icons in a folder, and the reference to which favicon.ico file to use is just generated dynamically along with the HTML page. Thoughts?

like image 350
SqlRyan Avatar asked Nov 04 '08 04:11

SqlRyan


People also ask

How do I change the favicon in CSS?

You can't set a favicon from CSS - if you want to do this explicitly you have to do it in the markup as you described. Most browsers will, however, look for a favicon. ico file on the root of the web site - so if you access http://example.com most browsers will look for http://example.com/favicon.ico automatically.

How do I change my favicon in HTML?

To add a favicon to your website, either save your favicon image to the root directory of your webserver, or create a folder in the root directory called images, and save your favicon image in this folder.


1 Answers

Why not?

var link = document.querySelector("link[rel~='icon']"); if (!link) {     link = document.createElement('link');     link.rel = 'icon';     document.getElementsByTagName('head')[0].appendChild(link); } link.href = 'https://stackoverflow.com/favicon.ico'; 
like image 145
keparo Avatar answered Sep 18 '22 06:09

keparo