[英]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.