我有一个网络应用程序,根据当前登录的用户进行品牌定制。我想将页面的favicon更改为专属标签的Logo,但是我无法找到任何代码或任何如何实现这一点的示例。有人以前成功过吗?
我考虑在一个文件夹中放置十几个图标,并动态生成与HTML页面一起使用的favicon.ico文件的引用。您有什么想法?
我考虑在一个文件夹中放置十几个图标,并动态生成与HTML页面一起使用的favicon.ico文件的引用。您有什么想法?
为什么不呢?
var link = document.querySelector("link[rel~='icon']");
if (!link) {
link = document.createElement('link');
link.rel = 'icon';
document.head.appendChild(link);
}
link.href = 'https://stackoverflow.com/favicon.ico';
rel
属性中删除shortcut
。 [shortcut
是IE专有的无效链接关系!](http://mathiasbynens.be/notes/rel-shortcut-icon) - Mathias Bynens以下代码适用于Firefox、Opera和Chrome(与这里发布的所有其他答案不同)。 这是一个在IE11中也可以正常工作的代码演示。以下示例可能无法在Safari或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);
}
然后您可以使用它如下:
var btn = document.getElementsByTagName('button')[0];
btn.onclick = function() {
changeFavicon('http://www.google.com/favicon.ico');
};
document.head || document.head = document.getElementsByTagName('head')[0];
Uncaught ReferenceError: Invalid left-hand side in assignment
。 - Patrick<link id="favicon" rel="shortcut icon" type="image/png" href="favicon.png" />
你可以使用 JavaScript 通过更改此链接上的 HREF 元素来更改网站的 favicon。例如(假设您正在使用 JQuery):
$("#favicon").attr("href","favicon2.png");
你还可以创建一个Canvas元素,并将其HREF设置为Canvas的ToDataURL(),就像Favicon Defender一样。
favicon.png
。这可能需要通过服务器端完成。 - cHaodocument.getElementById('favicon').setAttribute('href','favicon2.png')
更改#favicon
的href
属性。或许您可以将其添加到您的帖子中@fserb? - johannchopinjQuery 版本:
$("link[rel='shortcut icon']").attr("href", "favicon.ico");
或者更好的是:
$("link[rel*='icon']").attr("href", "favicon.ico");
原生 JavaScript 版本:
document.querySelector("link[rel='shortcut icon']").href = "favicon.ico";
document.querySelector("link[rel*='icon']").href = "favicon.ico";
一种更现代的方法:
const changeFavicon = link => {
let $favicon = document.querySelector('link[rel="icon"]')
// If a <link rel="icon"> element already exists,
// change its href to the given link.
if ($favicon !== null) {
$favicon.href = link
// Otherwise, create a new element and append it to <head>.
} else {
$favicon = document.createElement("link")
$favicon.rel = "icon"
$favicon.href = link
document.head.appendChild($favicon)
}
}
您可以像这样使用它:changeFavicon("http://www.stackoverflow.com/favicon.ico")
这里是一小段代码片段,可以将网站图标变成表情符号或文本。当我在stackoverflow的控制台时,它能够正常工作。
function changeFavicon(text) {
const canvas = document.createElement('canvas');
canvas.height = 64;
canvas.width = 64;
const ctx = canvas.getContext('2d');
ctx.font = '64px serif';
ctx.fillText(text, 0, 64);
const link = document.createElement('link');
const oldLinks = document.querySelectorAll('link[rel="shortcut icon"]');
oldLinks.forEach(e => e.parentNode.removeChild(e));
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = canvas.toDataURL();
document.head.appendChild(link);
}
changeFavicon('❤️');
网站的favicon可以在head标签中声明,例如:
<link rel="shortcut icon" type="image/ico" href="favicon.ico">
以下是我用于为Opera、Firefox和Chrome添加动态favicon支持的代码。但是,我无法让IE或Safari正常工作。基本上,Chrome允许动态favicon,但据我所知,它只在页面位置(或其中的iframe
等)更改时更新它们:
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]}
favicon.change("ICON URL")
。感谢我基于此代码的来源 http://softwareas.com/dynamic-favicons。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());
在大多数情况下,favicon 是这样声明的。
<link rel="icon" href"...." />
通过这种方式,您可以使用此方法引用它。
const linkElement = document.querySelector('link[rel=icon]');
你可以使用这个来更换图片
linkElement.href = 'url/to/any/picture/remote/or/relative';