簡體   English   中英

當用戶離開我的網站時如何更改瀏覽器中的標簽名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM