imarkdesigns
9/1/2017 - 7:35 AM

jQuery Browser Tab Title Modifier #jquery

jQuery Browser Tab Title Modifier #jquery

var initTabTitle = function() {

  var $doc = $(document),
      $body = $("body"),
      $window = $(window);
  var titleTimer = 0;
  var titleTimerRunning = false;
  var origTitle = $doc.find("title").text();
  var newTitle = "Don't forget to read this...";
  var isOrig = true;

  function swapTitle(){
    isOrig = ! isOrig;
    var t = isOrig ? origTitle : newTitle;
    setTitle( t );
  }

  function setTitle( t ){
    document.title = t;
  }

  $window
    .on("focus", function() {
    
  	setTitle( origTitle );

    // ugly hack because of a bug in Chrome which causes a change of
    // document.title immediately after tab switch
    // to have no effect on the browser title
    setTimeout(function() {
      setTitle( '.' );
      setTitle( origTitle );
    }, 1000);

  }).on("blur", function() {
    
  	setTitle( newTitle );
  
  });  
  
}

$(document).ready( initTabTitle() );