How to change website favicon dynamically

0 votes
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?
Sep 21, 2020 in Java-Script by kartik
• 37,530 points
870 views

2 answers to this question.

0 votes

Hello @kartik,

Yes,you can do it by using the following code:

(function() {
    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = 'http://www.edureka.co/favicon.ico';
    document.getElementsByTagName('head')[0].appendChild(link);
})();

Hope it helps!!
Thank you!!

answered Sep 21, 2020 by Niroj
• 82,820 points
0 votes

There is a way of doing it through javascript and the developer framework as shown below.

JavaScript

$('head').append('<link id="dynamic-favicon" rel="shortcut icon" href="https://www.XYZ.com/favicon.ico" />');

This works by adding a link to the head of the page, which changes the shortcut icon of the page to the specified image. Causing the image on the tab in the browser to change.

answered Sep 28, 2020 by anju
• 160 points

Related Questions In Java-Script

0 votes
2 answers

How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

For security reasons browsers do not allow ...READ MORE

answered Dec 16, 2020 in Java-Script by Rajiv
• 8,890 points
28,843 views
0 votes
1 answer

How to dynamically load JS inside JS?

Hello @kartik, The script is not added to ...READ MORE

answered Aug 25, 2020 in Java-Script by Niroj
• 82,820 points
1,256 views
0 votes
1 answer

How to change an element's ID with jQuery?

Hello @kartik, Your syntax is incorrect, you should ...READ MORE

answered Aug 25, 2020 in Java-Script by Niroj
• 82,820 points
906 views
0 votes
2 answers
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,872 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
1,351 views
0 votes
2 answers

How can we create a session in PHP?

Hello, niroj. Here is my idea session_start(); $_SESSION['USERNAME'] ...READ MORE

answered Dec 7, 2020 in PHP by Famous
• 140 points
254 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,820 points
459 views
0 votes
1 answer

How to change url after success in ajax without page reload?

Hello @kartik, Use the browser history to change ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,820 points
5,944 views
0 votes
1 answer

How to implement useEffect() so the list on the front page updates only when a name change has been detected?

Hello @kartik, Instead of passing the entire object ...READ MORE

answered Jun 1, 2020 in Java-Script by Niroj
• 82,820 points
212 views