JavaScript:在运行时更改favicon不会';t在弹出窗口上工作
我想在运行时更改favicon。我的代码在选项卡中打开页面时有效,但在弹出窗口中打开页面时无效 我已经在最新的Chrome、Firefox和Edge上进行了测试,结果不起作用,但可以在InternetExplorer11上使用 这是用于更改favicon的代码:JavaScript:在运行时更改favicon不会';t在弹出窗口上工作,javascript,html,popup,favicon,Javascript,Html,Popup,Favicon,我想在运行时更改favicon。我的代码在选项卡中打开页面时有效,但在弹出窗口中打开页面时无效 我已经在最新的Chrome、Firefox和Edge上进行了测试,结果不起作用,但可以在InternetExplorer11上使用 这是用于更改favicon的代码: function changeFavicon(src) { // delete the current favicon from the DOM var oldLink = document.getElementBy
function changeFavicon(src) {
// delete the current favicon from the DOM
var oldLink = document.getElementById('appFavicon');
if (oldLink) {
document.head.removeChild(oldLink);
}
// add the new favicon
var link = document.createElement('link');
link.id = 'appFavicon';
link.rel = 'shortcut icon';
link.href = src;
document.head.appendChild(link);
}
这是为gif制作的完整的favicon.html
:
<!doctype html>
<html>
<head>
<link rel="shortcut icon" id="appFavicon" href="favicon.ico">
</head>
<body>
<script>
function popUp(url,windowName) {
newwindow=window.open(url,windowName,'height=200,width=350');
if (window.focus) {newwindow.focus()}
return false;
}
function changeFavicon(src) {
var oldLink = document.getElementById('appFavicon');
if (oldLink) {
document.head.removeChild(oldLink);
}
var link = document.createElement('link');
link.id = 'appFavicon';
link.rel = 'shortcut icon';
link.href = src;
document.head.appendChild(link);
}
</script>
<p><button onclick="changeFavicon('https://c5-excel-15.cdn.office.net/x/_layouts/resources/FavIcon_Excel.ico')">Excel</button></p>
<p><button onclick="changeFavicon('https://c5-powerpoint-15.cdn.office.net/p/resources/1033/FavIcon_Ppt.ico')">Power Point</button></p>
<p><button onclick="changeFavicon('https://c5-word-view-15.cdn.office.net/wv/resources/1033/FavIcon_Word.ico')">Word</button>
<br /><br />
<button onclick="popUp('favicon.html')">Open this page in a PopUp</button></p>
</body>
</html>
功能弹出窗口(url、windowName){
newwindow=window.open(url,windowName,'height=200,width=350');
if(window.focus){newwindow.focus()}
返回false;
}
功能更改Favicon(src){
var oldLink=document.getElementById('appFavicon');
如果(旧链接){
document.head.removeChild(oldLink);
}
var link=document.createElement('link');
link.id='appFavicon';
link.rel='快捷方式图标';
link.href=src;
document.head.appendChild(链接);
}
胜过
电源点
话
在弹出窗口中打开此页面
这个问题很有趣。这能回答你的问题吗@谢谢,但不,我需要在运行时更改..@chandukomati nope。。。在Firefox上也不起作用!弹出窗口没有图标。我不太确定它在windows上是如何出现的,但左上角的chrome徽标始终保持在那里(在linux上),这也是您在示例弹出窗口中看到的。默认的favicon是灰色球体(您在标准页面上看到的)。在弹出窗口中只有chrome徽标,我很确定你可以像默认的favicon一样轻松地更改它。希望这能澄清你的问题。