亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Javascript 刪除非自托管網站上的網站圖標?

如何使用 Javascript 刪除非自托管網站上的網站圖標?

阿晨1998 2023-12-11 16:06:59
<link rel="shortcut icon" href="https://files.writeas.org/favicon.ico" />是網站中的代碼嗎,我無法更改它,我只能更改 CSS 或注入我自己的 Javascript,那么我如何使用 Javascript 獲取這部分 HTML 代碼并注釋掉/覆蓋它從而刪除它?
查看完整描述

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)


查看完整回答
反對 回復 2023-12-11
?
喵喵時光機

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)


查看完整回答
反對 回復 2023-12-11
  • 2 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號