简体   繁体   English

如果浏览器选项卡处于活动状态,如何更改Shopify标题

[英]How to change Shopify Title if the Browser tab is active or not

I'm trying to accomplish this in Shopify. 我正在努力在Shopify中实现这一目标。

If the user has the browser tab active (change the page title to message A) and if the tab is not active (change page title to show message B). 如果用户的浏览器选项卡处于活动状态(将页面标题更改为消息A),并且该选项卡未处于活动状态(将页面标题更改为显示消息B)。

For some reason, something is missing or wrong in my code. 由于某些原因,我的代码中缺少某些内容或发生了错误。

I'm inserting this code in the header of the file: theme.liquid 我将此代码插入文件的标题中:theme.liquid

<script>
      $(function () {
          leftTitle = 'Message A'; 
          backTitle = 'Message B';

          $(window).blur(function () {
              page.title = leftTitle;
          });

          $(window).focus(function () {
              page.title = backTitle;
          });
      });

    </script>

Any advice? 有什么建议吗?

I think you're trying to use document.title = leftTitle; 我认为您正在尝试使用document.title = leftTitle; and document.title = backTitle; document.title = backTitle;

page doesn't exist natively on browsers, unless you've declared it before, so page.title doesn't change anything; page在浏览器中本机不存在,除非您之前已经声明过,所以page.title不会改变任何内容; it should give you a console error. 它应该给您一个控制台错误。

 var leftTitle = 'Blurred'; var backTitle = 'Focused'; window.onblur = function () { console.log("Blur"); document.title = leftTitle; }; window.onfocus = function () { console.log("Focus"); document.title = backTitle; }; console.log("Event listeners initialized."); 

I found the solution myself: 我自己找到了解决方案:

<script>
  var message = "Insert your message here";
  var original = document.title;

  window.onblur = function () { document.title = message; }
  window.onfocus = function () { document.title = original; }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在选项卡更改时为浏览器选项卡标题的位置设置动画 - How to animate position of browser tab's title on tab change 选项卡bootstarp处于活动状态时如何更改标题文本? - How can change text of title when tab bootstarp was been active? 如何在浏览器扩展中更改另一个 window 上的活动选项卡? - How to change the active a tab on another window in a browser extension? 从控制台更改浏览器中的活动选项卡 - Change the active tab in browser from console 当用户转到其他选项卡或最小化浏览器时,如何更改和闪烁浏览器的标题? - How to change and blink the title of the browser when the user goes to other tab or minimizes the browser? 当标签处于活动/非活动状态时,如何让标签更改其标题? - How can I make an tab change it's title when it's active/inactive? 如何浏览器标签标题将其设置为滚动/移动? - How to Browser tab title set it to scrolling/moving? 如何在浏览器选项卡标题中显示完整的文本 - How to show the complete text in the browser tab title 如何设置新浏览器标签的标题? - How to set the title for the new browser tab? 如何识别浏览器上的最后一个活动标签 - How To Identify Last Active Tab On Browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM