Ad

How To Change Shopify Title If The Browser Tab Is Active Or Not

- 1 answer

I'm trying to accomplish this in 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).

For some reason, something is missing or wrong in my code.

I'm inserting this code in the header of the file: 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?

Ad

Answer

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>
Ad
source: stackoverflow.com
Ad