最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Can't change favicon with javascript in safari - Stack Overflow

programmeradmin6浏览0评论

I have a page that changes the favicon through javascript. This works great on chrome and firefox, but on Safari it does not work. I even added the <link rel="apple-touch-icon" href="favicon-1.png"> part and changed that through javascript as well, but that also does not do the trick.

The page looks like this:

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Foo</title>
  <link rel="icon" type="image/png" href="favicon-1.png">
  <link rel="apple-touch-icon" href="favicon-1.png">

  <style>
  </style>
</head>
<body></body>
    <script>
        setTimeout(() => {
          var favicon = document.querySelector('link[rel="icon"]');
          var appleTouchIcon = document.querySelector('link[rel="apple-touch-icon"]');
          
          favicon.setAttribute('type', 'image/png');
          favicon.setAttribute('href', 'favicon-2.png');

          appleTouchIcon.setAttribute('href', 'favicon-2.png');
          }, 4000);
    </script>
</body>
</html>

Can anyone spot what I am doing wrong here?

I have a page that changes the favicon through javascript. This works great on chrome and firefox, but on Safari it does not work. I even added the <link rel="apple-touch-icon" href="favicon-1.png"> part and changed that through javascript as well, but that also does not do the trick.

The page looks like this:

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Foo</title>
  <link rel="icon" type="image/png" href="favicon-1.png">
  <link rel="apple-touch-icon" href="favicon-1.png">

  <style>
  </style>
</head>
<body></body>
    <script>
        setTimeout(() => {
          var favicon = document.querySelector('link[rel="icon"]');
          var appleTouchIcon = document.querySelector('link[rel="apple-touch-icon"]');
          
          favicon.setAttribute('type', 'image/png');
          favicon.setAttribute('href', 'favicon-2.png');

          appleTouchIcon.setAttribute('href', 'favicon-2.png');
          }, 4000);
    </script>
</body>
</html>

Can anyone spot what I am doing wrong here?

Share Improve this question edited Oct 10, 2020 at 15:49 Rogier de Ruijter asked Sep 7, 2020 at 17:34 Rogier de RuijterRogier de Ruijter 9861 gold badge10 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It seems like it is not even possible as briefly mentioned in this webkit issue.

Citing:

...Since we quite purposefully don't allow dynamic changes/animations to the favicon...

Go to your page in safari and press option + mand + e to 'EMPTY CACHE' or go to Develop/Empty Caches.

That will solve the problem of the favicon that doesn't change.

发布评论

评论列表(0)

  1. 暂无评论