kep*_*aro 358
为什么不?
(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);
})();
Run Code Online (Sandbox Code Playgroud)
Firefox应该很酷.
编辑以正确覆盖现有图标
Mat*_*ens 85
这里有一些适用于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);
}
Run Code Online (Sandbox Code Playgroud)
然后您将按如下方式使用它:
var btn = document.getElementsByTagName('button')[0];
btn.onclick = function() {
changeFavicon('http://www.google.com/favicon.ico');
};
Run Code Online (Sandbox Code Playgroud)
fse*_*erb 44
如果您有以下HTML代码段:
<link id="favicon" rel="shortcut icon" type="image/png" href="favicon.png" />
Run Code Online (Sandbox Code Playgroud)
例如,您可以通过更改此链接上的HREF元素来使用Javascript更改favicon(假设您使用的是JQuery):
$("#favicon").attr("href","favicon2.png");
Run Code Online (Sandbox Code Playgroud)
您还可以创建Canvas元素并将HREF设置为画布的ToDataURL(),就像Favicon Defender一样.
vor*_*laz 33
jQuery版本:
$("link[rel='shortcut icon']").attr("href", "favicon.ico");
Run Code Online (Sandbox Code Playgroud)
甚至更好:
$("link[rel*='icon']").attr("href", "favicon.ico");
Run Code Online (Sandbox Code Playgroud)
香草JS版:
document.querySelector("link[rel='shortcut icon']").href = "favicon.ico";
document.querySelector("link[rel*='icon']").href = "favicon.ico";
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 13
更现代的方法:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
changeFavicon("http://www.stackoverflow.com/favicon.ico")
Run Code Online (Sandbox Code Playgroud)
Jef*_*don 10
favicon在head标签中声明如下:
<link rel="shortcut icon" type="image/ico" href="favicon.ico">
Run Code Online (Sandbox Code Playgroud)
您应该只能在视图数据中传递所需图标的名称并将其放入head标记中.
这是我用来为Opera,Firefox和Chrome添加动态favicon支持的一些代码.我无法让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]}
Run Code Online (Sandbox Code Playgroud)
要更改favicon,只需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());
Run Code Online (Sandbox Code Playgroud)
道具https://koddsson.com/posts/emoji-favicon/
这是使收藏夹图标成为表情符号或文本的片段。当我在 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('??');
Run Code Online (Sandbox Code Playgroud)
小智 5
我会使用Greg的方法并为favicon.ico制作一个自定义处理程序.这是一个(简化的)处理程序,它可以工作:
using System;
using System.IO;
using System.Web;
namespace FaviconOverrider
{
public class IcoHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/x-icon";
byte[] imageData = imageToByteArray(context.Server.MapPath("/ear.ico"));
context.Response.BinaryWrite(imageData);
}
public bool IsReusable
{
get { return true; }
}
public byte[] imageToByteArray(string imagePath)
{
byte[] imageByteArray;
using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
imageByteArray = new byte[fs.Length];
fs.Read(imageByteArray, 0, imageByteArray.Length);
}
return imageByteArray;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在IIS6中的Web配置的httpHandlers部分中使用该处理程序,或使用IIS7中的"处理程序映射"功能.
小智 5
大多数情况下,favicon 是这样声明的。
<link rel="icon" href"...." />
Run Code Online (Sandbox Code Playgroud)
这样你就可以通过 this 获得对它的引用。
<link rel="icon" href"...." />
Run Code Online (Sandbox Code Playgroud)
你可以用这个改变图片
linkElement.href = 'url/to/any/picture/remote/or/relative';
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
165459 次 |
最近记录: |