2 回答
TA贡献1862条经验 获得超6个赞
如果您无法删除它,您可以使用以下代码更改它(注意:删除可能还不够,因为许多浏览器默认为 /favicon.ico)
//const ico_url = document.location.origin + '/favicon.ico'
const ico_url = 'https://api.adorable.io/avatar/256/your_icon.png'
let head = document.getElementsByTagName('head')[0]
console.dir(head)
var link;
let links = head.getElementsByTagName('link')
for(let i=0; i<links.length; i++) {
if (links[i].rel == 'icon' || links[i].rel == 'shortcut icon') {
link = links[i]
console.log('old-icon: '+link.href)
link.href = ico_url
console.log('new-icon: '+link.href)
}
}
if (typeof(link) == 'undefined') {
console.log('creating favicon link !')
link = document.createElement('link')
link.setAttribute('rel','icon')
link.setAttribute('type','image/png')
link.setAttribute('href',ico_url) // new favicon ...
document.getElementsByTagName('head')[0].appendChild(link)
}
console.log(link.href)
TA贡献1846条经验 获得超7个赞
也许你可以尝试更换 head 标签...
let ico_url = 'https://api.adorable.io/avatar/256/head_icon.png'
var head = document.createElement("head");
link = document.createElement('link')
link.setAttribute('rel','icon')
link.setAttribute('type','image/png')
link.setAttribute('href',ico_url) // new favicon ...
head.appendChild(link)
const html = document.getElementsByTagName('html')[0]
html.appendChild(head)
html.replaceChild(head,document.getElementsByTagName('head')[0])
console.dir(head)
- 2 回答
- 0 关注
- 135 浏览
添加回答
举报