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)
}
}
这里's some code I use to add dynamic favicon support to Opera, Firefox and Chrome. I couldn' t得到IE或Safari工作 . 基本上Chrome允许动态的favicons,但只有当页面的位置(或其中的 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]}
14 回答
为什么不?
Firefox应该很酷 .
编辑以正确覆盖现有图标
这里有一些适用于Firefox,Opera和Chrome的代码(与此处发布的其他答案不同) . 这也是一个不同的demo of code that works in IE11 . 以下示例可能无法在Safari或Internet Explorer中使用 .
然后您将按如下方式使用它:
Fork away或view a demo .
如果您有以下HTML代码段:
例如,您可以通过更改此链接上的HREF元素来使用Javascript更改favicon(假设您使用的是JQuery):
您还可以创建Canvas元素并将HREF设置为画布的ToDataURL(),就像Favicon Defender一样 .
jQuery Version:
甚至更好:
Vanilla JS version:
更现代的方法:
然后你可以像这样使用它:
这里's some code I use to add dynamic favicon support to Opera, Firefox and Chrome. I couldn' t得到IE或Safari工作 . 基本上Chrome允许动态的favicons,但只有当页面的位置(或其中的
iframe
等)发生变化时才会更新它们:据我所知:要更改favicon,请使用上面的内容进入
favicon.change("ICON URL")
.(根据http://softwareas.com/dynamic-favicons获取代码我基于此 . )
favicon在head标签中声明如下:
您应该只能在视图数据中传递所需图标的名称并将其放入head标记中 .
我会使用Greg的方法并为favicon.ico制作一个自定义处理程序 . 这是一个(简化的)处理程序,它可以工作:
然后,您可以在IIS6中的Web配置的httpHandlers部分中使用该处理程序,或使用IIS7中的“处理程序映射”功能 .
使IE工作的唯一方法是将Web服务器设置为处理* .ico的请求以调用服务器端脚本语言(PHP,.NET等) . 还要设置* .ico以重定向到单个脚本,并让此脚本提供正确的favicon文件 . 我相信如果你想在不同的favicons之间在同一个浏览器中来回跳转,那么缓存仍然会有一些有趣的问题 .
根据WikiPedia,您可以使用
head
部分中的link
标记指定要加载的favicon文件,参数为rel="icon"
.例如:
我想如果你想为这个电话写一些动态内容,你就可以访问cookie,这样你就可以检索你的会话信息并提供适当的内容 .
您可能会违反文件格式(据报道IE只支持它的.ICO格式,而大多数其他人都支持PNG和GIF图像)以及可能的缓存问题,无论是在浏览器上还是通过代理 . 这可能是因为favicon的原始版本,特别是用于标记带有网站迷你徽标的书签 .
Yes totally possible
在favicon.ico之后使用 querystring (以及其他文件链接 - 请参阅下面的答案链接)
只需确保服务器使用正确的映像文件(可能是 static 路由规则或 dynamic 服务器端代码)响应"someUserId" .
例如
然后,您使用的任何服务器端语言/框架都应该能够轻松地找到基于userId的文件并提供它以响应该请求 .
But to do favicons properly (其实是 really 复杂的主题)请在这里看到答案https://stackoverflow.com/a/45301651/661584
A lot lot easier 而不是自己制定所有细节 .
请享用 .
对于那些使用jQuery的人来说,有一个单行解决方案:
或者,如果你想要一个表情符号:)
道具https://koddsson.com/posts/emoji-favicon/
我在我的项目中使用favico.js :)