![](/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.