您如何制作书签更改页面图标?

问题描述 投票:1回答:1

在我开始之前,这不是重复的。为了说明我的目标,我想要一个书签,该书签在单击时会更改页面图标,而无需加载新标签。其他类似问题也给您的小书签一个图标,但没有实现我想要的功能。

这是我现在拥有的代码:

data:text/html; charset=utf-8, <html><link rel="shortcut icon" href="http://link-to-my-icon.info/favicon.ico"></html>

它可以工作,但是会更改我的标签。我想保持选项卡打开,但是编辑HTML来更改页面图标。我尝试使用

javascript: document.write(<link rel="shortcut icon" href="http://transparent-favicon.info/favicon.ico">);

同样,但是它绝对不起作用,而我的第一次尝试至少改变了图标。

非常感谢您的帮助,谢谢。

javascript favicon bookmarklet
1个回答
3
投票

结帐this gistdemo

//source: https://gist.github.com/mathiasbynens/428626

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.