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,510 points
1,443 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,680 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,910 points
41,143 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,680 points
1,589 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,680 points
1,515 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,680 points
13,265 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,680 points
1,618 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
334 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,680 points
669 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,680 points
6,862 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,680 points
274 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP