JavaScript:在运行时更改网站图标不适用于弹出窗口

ar0*_*968 5 html javascript favicon popup

我想在运行时更改 favicon。当页面在选项卡中打开时,我的代码有效,但在弹出窗口中打开页面时不起作用

在此处输入图片说明

在线演示

我已经在最新的 Chrome、Firefox 和 Edge 上进行了测试,但不能正常工作,但可以在 Internet Explorer 11 上运行。

这是用于更改网站图标的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

这是favicon.html为 gif 制作的完整内容:

<!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>
Run Code Online (Sandbox Code Playgroud)