Advertisement
briank

Dynamic Focus/Blur Titles

Jan 5th, 2022
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     // Title when tab has focus
  3.     var fTitle = 'I promise, Jack!';
  4.     // Title rotation list when tab is "blurred" (doesn't have focus)
  5.     var bTitles = [
  6.     'Come back!',
  7.     '*blows whistle*'
  8.     ];
  9.     // Title rotation interval in milliseconds (e.g. 2000 = 2 seconds)
  10.     var interval = 2000;
  11.     var bti = 0, iv;
  12.  
  13.     function onFocus () {
  14.     if (iv) {
  15.         clearInterval(iv);
  16.         iv = null;
  17.     }
  18.     document.title = fTitle;
  19.     }
  20.  
  21.     function onBlur () {
  22.     document.title = bTitles[bti++ % bTitles.length];
  23.     if (bTitles.length > 1 && !iv) iv = setInterval(onBlur, interval);
  24.     }
  25.  
  26.     document.title = fTitle;
  27.     if (window.addEventListener) {
  28.     addEventListener('focus', onFocus);
  29.     addEventListener('blur', onBlur);
  30.     } else if (window.attachEvent) {
  31.     attachEvent('onfocus', onFocus);
  32.     attachEvent('onblur', onBlur);
  33.     }
  34. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement