动态更改网站图标 [英] Changing website favicon dynamically

查看:36
本文介绍了动态更改网站图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,该应用程序根据当前登录的用户进行标记.我想将页面的收藏图标更改为私有标签的徽标,但我找不到任何代码或任何如何做到这一点的例子.以前有人成功过吗?

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?

我想象在一个文件夹中有十几个图标,并且对要使用的 favicon.ico 文件的引用是随 HTML 页面一起动态生成的.想法?

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?

推荐答案

为什么不呢?

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';

这篇关于动态更改网站图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆