![](/img/trans.png)
[英]How to change and blink the title of the browser when the user goes to other tab or minimizes the browser?
[英]How to change tab name in browser when user goes off from my site
所以我正在制作一个网站,一切都做得很好,但我不知道很多关于 javascript 的事情。
我正在寻找可以帮助我解决这个问题的东西,并找到了一些类似的东西,但在我的情况下不起作用。
这是问题/想法:
因此,如果有人可以帮助我编写代码并稍微解释一下。
谢谢你。
您需要使用窗口对象的 onblur 和 onfocus 事件。
所以像这样(这是原生的javascript,没有jquery)。
<script>
window.onblur = function () { document.title = 'you went?'; }
window.onfocus = function () { document.title = 'you came back'; }
</script>
$(window).focus(function() {
document.title = 'defult title';
});
$(window).blur(function() {
document.title = 'you went?';
});
您可以使用 :
window.addEventListener('blur',function(){
alert("please Came Back");
document.head.title = "your tilte"
});
window.addEventListener('focus',function(){
alert("Hi");
document.head.title = "your tilte"
});
对我来说最好的是使用:
window.onload = function() {
var pageTitle = document.title;
var attentionMessage = 'Come Back!';
document.addEventListener('visibilitychange', function(e) {
var isPageActive = !document.hidden;
if(!isPageActive){
document.title = attentionMessage;
}else {
document.title = pageTitle;
}
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.