12

Problem exists only on FireFox (from 3.6 up to current 9), other browsers are fine. My code looks like this:

jQuery.extend({
    AnchorFromUrl : function(url) {
        var anchor = url.substr(1).replace('.html','');
        $.fizzer_anchor = anchor;
        window.location.hash = anchor;
        return anchor;
    }
});

The most weird thing is that if I place an alert before the window.location.hash = anchor; line, after clicking Ok favicon doesn't disappear, remove that alert() and you get your favicon disappearing.

Note: it also drops the favicon if you just do window.location = something.

Tomas
  • 57,621
  • 49
  • 238
  • 373
Psihius
  • 143
  • 1
  • 1
  • 7

3 Answers3

28

I had the same problem, but found this interesting post and it worked for me, its just adding 2 lines of javascript. The problem occure when the hash element changes, so, we need to re-stablish it via javascript

http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/

this is the code

function setFavicon() {
  var link = $('link[type="image/x-icon"]').remove().attr("href");
  $('<link href="'+ link +'" rel="shortcut icon" type="image/x-icon" />').appendTo('head');
}

Or (thanks to Mottie) using jQuery detach

$('link[type*=icon]').detach().appendTo('head');
Raphael Isidro
  • 932
  • 10
  • 19
  • my version of jQuery (1.6.4) doesnt seem to support the type=image/x-icon syntax, so I changed it to type*=icon. Any better suggestions? – Mansiemans Oct 27 '11 at 10:18
  • @Mansiemans, try to use quotes for the attribute value: `$("link[type='image/x-icon']")` – Tomas Jan 24 '12 at 10:02
  • 3
    A more generally applicable selector would be `$('link[rel~="icon"]')`. – cmbuckley Jun 19 '12 at 09:25
  • 3
    I'm still having this problem in Firefox 14.0.1 and I've found that `detach()` works just as nicely: `$('link[type*=icon]').detach().appendTo('head');` – Mottie Aug 03 '12 at 17:49
1

It worked for me :

var link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = 'FAV_ICON_URL';
document.getElementsByTagName('head')[0].appendChild(link);

Refer : Changing Website Icon Dynamically

Community
  • 1
  • 1
Sanjay Kumar
  • 1,474
  • 14
  • 22
-5

I noticed this behaviour, too. Every now and then Firefox drops a favicon or it refuses to put the favicon alongside my bookmark. I think this is a Firefox bug.

To workaround this (and for other functionality), I installed the Favicon Picker add-on. Of course, this doesn't solve your problem on other computers, like clients and the like.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • 4
    yes, it is a [firefox bug](https://bugzilla.mozilla.org/show_bug.cgi?id=519028). This is not an answer. @Phisius I wonder why this was accepted, the other answer should be. – Tomas Jan 24 '12 at 10:05