Changing website favicon dynamically

前端 未结 15 1305
温柔的废话
温柔的废话 2020-11-22 02:17

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,

相关标签:
15条回答
  • 2020-11-22 02:23

    Why not?

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

    Firefox should be cool with it.

    edited to properly overwrite existing icons

    0 讨论(0)
  • 2020-11-22 02:23

    Here's some code I use to add dynamic favicon support to Opera, Firefox and Chrome. I couldn't get IE or Safari working though. Basically Chrome allows dynamic favicons, but it only updates them when the page's location (or an iframe etc in it) changes as far as I can tell:

    var IE = navigator.userAgent.indexOf("MSIE")!=-1
    var favicon = {
        change: function(iconURL) {
            if (arguments.length == 2) {
                document.title = optionalDocTitle}
            this.addLink(iconURL, "icon")
            this.addLink(iconURL, "shortcut icon")
    
            // Google Chrome HACK - whenever an IFrame changes location 
            // (even to about:blank), it updates the favicon for some reason
            // It doesn't work on Safari at all though :-(
            if (!IE) { // Disable the IE "click" sound
                if (!window.__IFrame) {
                    __IFrame = document.createElement('iframe')
                    var s = __IFrame.style
                    s.height = s.width = s.left = s.top = s.border = 0
                    s.position = 'absolute'
                    s.visibility = 'hidden'
                    document.body.appendChild(__IFrame)}
                __IFrame.src = 'about:blank'}},
    
        addLink: function(iconURL, relValue) {
            var link = document.createElement("link")
            link.type = "image/x-icon"
            link.rel = relValue
            link.href = iconURL
            this.removeLinkIfExists(relValue)
            this.docHead.appendChild(link)},
    
        removeLinkIfExists: function(relValue) {
            var links = this.docHead.getElementsByTagName("link");
            for (var i=0; i<links.length; i++) {
                var link = links[i]
                if (link.type == "image/x-icon" && link.rel == relValue) {
                    this.docHead.removeChild(link)
                    return}}}, // Assuming only one match at most.
    
        docHead: document.getElementsByTagName("head")[0]}
    

    To change favicons, just go favicon.change("ICON URL") using the above.

    (credits to http://softwareas.com/dynamic-favicons for the code I based this on.)

    0 讨论(0)
  • 2020-11-22 02:27

    According to WikiPedia, you can specify which favicon file to load using the link tag in the head section, with a parameter of rel="icon".

    For example:

     <link rel="icon" type="image/png" href="/path/image.png">
    

    I imagine if you wanted to write some dynamic content for that call, you would have access to cookies so you could retrieve your session information that way and present appropriate content.

    You may fall foul of file formats (IE reportedly only supports it's .ICO format, whilst most everyone else supports PNG and GIF images) and possibly caching issues, both on the browser and through proxies. This would be because of the original itention of favicon, specifically, for marking a bookmark with a site's mini-logo.

    0 讨论(0)
  • 2020-11-22 02:28

    If you have the following HTML snippet:

    <link id="favicon" rel="shortcut icon" type="image/png" href="favicon.png" />
    

    You can change the favicon using Javascript by changing the HREF element on this link, for instance (assuming you're using JQuery):

    $("#favicon").attr("href","favicon2.png");
    

    You can also create a Canvas element and set the HREF as a ToDataURL() of the canvas, much like the Favicon Defender does.

    0 讨论(0)
  • 2020-11-22 02:28

    Or if you want an emoticon :)

    var canvas = document.createElement("canvas");
    canvas.height = 64;
    canvas.width = 64;
    
    var ctx = canvas.getContext("2d");
    ctx.font = "64px serif";
    ctx.fillText("☠️", 0, 64); 
    
    $("link[rel*='icon']").prop("href", canvas.toDataURL());
    

    Props to https://koddsson.com/posts/emoji-favicon/

    0 讨论(0)
  • 2020-11-22 02:32

    Here’s some code that works in Firefox, Opera, and Chrome (unlike every other answer posted here). Here is a different demo of code that works in IE11 too. The following example might not work in Safari or Internet Explorer.

    /*!
     * Dynamically changing favicons with JavaScript
     * Works in all A-grade browsers except Safari and Internet Explorer
     * Demo: http://mathiasbynens.be/demo/dynamic-favicons
     */
    
    // HTML5™, baby! http://mathiasbynens.be/notes/document-head
    document.head = document.head || document.getElementsByTagName('head')[0];
    
    function changeFavicon(src) {
     var link = document.createElement('link'),
         oldLink = document.getElementById('dynamic-favicon');
     link.id = 'dynamic-favicon';
     link.rel = 'shortcut icon';
     link.href = src;
     if (oldLink) {
      document.head.removeChild(oldLink);
     }
     document.head.appendChild(link);
    }
    

    You would then use it as follows:

    var btn = document.getElementsByTagName('button')[0];
    btn.onclick = function() {
     changeFavicon('http://www.google.com/favicon.ico');
    };
    

    Fork away or view a demo.

    0 讨论(0)
提交回复
热议问题