Change content of title tag, when switching to other open tab in the browser

扶醉桌前 提交于 2020-08-17 17:49:49

问题


I just recently saw this on two different websites, does anyone know how it is done? If you have multiple tabs open, and you leave the current one, the title in the tab is changed. Very nice trick!

http://blog.invisionapp.com/

http://zerosixthree.se/create-a-responsive-header-video-with-graceful-degradation/


回答1:


This works by registering Handlers on the onfocus and onblur events of window.

jQuery-Style:

$(window).on('blur', function() { ... });

Without jQuery:

window.onblur = function() { ... }

If that was not clear: the pages title can be read/written via document.title




回答2:


$(function() {
  var message = "Don't forget us";
  var original;

  $(window).focus(function() {
    if (original) {
      document.title = original;
    }
  }).blur(function() {
    var title = $('title').text();
    if (title != message) {
      original = title;
    }
    document.title = message;
  });
});



回答3:


Originally Coded by: https://flintobox.com/blog/

  $(function () {
    var message = "Hey, come back!";
    var original;

    $(window).focus(function() {
      if(original) {
        document.title = original;
      }
    }).blur(function() {
      var title = $('title').text();
      if(title != message) {
        original = title;
      }
      document.title = message;
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


来源:https://stackoverflow.com/questions/23878277/change-content-of-title-tag-when-switching-to-other-open-tab-in-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!