繁体   English   中英

点击标题栏

[英]Flash the title bar

我尝试使用非常基本的HTML将标题栏设置为flash。 我已经给了它一个镜头,但下面的代码似乎不起作用,我不知道为什么。 此外,有没有办法使标题栏闪存文本,但只有当用户没有查看当前的浏览器页面?

我的尝试:

function Flash() {
            window.setTimeout(function() {
            alert(document.title);
                document.title = (document.title == "Company" ? "Company - flash text" : "Company");
            }, 1000);

            this.stop = function() { 
                document.title = "Company";
                clearTimeout(this.timer);
            }
        }

这个重复使我的浏览器闪烁作为指示

但是因为我现在正在编写这个脚本:

https://plungjan.name/SO/flashtitle.html

<script>
var timer="";
var isBlurred=false;
window.onblur=function() {
  isBlurred = true;
  timer=window.setInterval(function() {
    document.title = document.title == "Company" ? "Company - flash text" : "Company";
  }, 1000);
}
window.onfocus=function() { 
  isBlurred = false;
  document.title = "Company";
  clearInterval(timer);
}
</script>

jQuery版本并没有太大的不同(但没有经过测试)

var timer="";
var isBlurred=false;
$(window).on("blur",function() {
  isBlurred = true;
  timer=window.setInterval(function() {
    document.title = document.title == "Company" ? "Company - flash text" : "Company";
  }, 1000);
}).on("focus",function() { 
  isBlurred = false;
  document.title = "Company";
  clearInterval(timer);
});

当访问者没有查看当前浏览器页面时(窗口隐藏,模糊),您询问有关闪烁(闪烁)document.title栏的问题。 所以我们必须设置两个功能。 首先,我们需要检测浏览器窗口当前是否处于活动状态 - visibilityChange(actionFunction)。 第二,我们需要开始获取flashing文件。标题栏 - comeBackAlerts()。 在这里 - 解决方案对我来说很好,希望和你。

/* Set event leaving the page to execute actionFunction */

function visibilityChange(actionFunction) {

  window.focus(); /* window.onfocus = infoIn;  */

  var hidden = "hidden";

  /* Standards: */
  if (hidden in document) {
    document.addEventListener("visibilitychange", actionFunction);
  } else if ((hidden = "mozHidden") in document) {
    document.addEventListener("mozvisibilitychange", actionFunction);
  } else if ((hidden = "webkitHidden") in document) {
    document.addEventListener("webkitvisibilitychange", actionFunction);
  } else if ((hidden = "msHidden") in document) {
    document.addEventListener("msvisibilitychange", actionFunction);
  }
  /* IE 9 and lower: */
  else if ("onfocusin" in document) {
    document.onfocusin = document.onfocusout = actionFunction;
  }
  /* All others: */
  else {
    window.onpageshow = window.onpagehide = window.onfocus = window.onblur = actionFunction;
  }
}


/* Function to make browser window blink in task bar */

var comeBackAlerts = (function() {
  var oldTitle = document.getElementsByTagName('h1')[0].innerText; /* document.title; */
  var msg = "Arbir.ru";
  var intervalId;
  var blink = function() {
    intervalId = setInterval(function() {
      /* document.title = document.title == msg ? ' ' : msg; */
      if (document.title == msg) {
        document.title = oldTitle;
      } else {
        document.title = msg;
      }
    }, 1000);
  };
  var clear = function() {
    clearInterval(intervalId);
    document.title = oldTitle;
    window.onmousemove = null;
    window.onmouseout = null;
    intervalId = null;
  };
  return function() {
    if (!intervalId) {
      blink();
      window.onmousemove = clear;
    }
  };
}());

/* Running the functions */

visibilityChange(comeBackAlerts);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM