簡體   English   中英

Window.onbeforeunload 頁面刷新后未設置

[英]Window.onbeforeunload not setting after page refresh

我正在制作一個捕獲錯誤的錯誤報告器,然后就在用戶刷新/關閉之前,它將錯誤發送到服務器以放入日志文件中。 我有一個window.onerror function 捕獲錯誤並將其存儲在數組中。 window.onerror function 中,我還設置了 window.onbeforeunload,它將向 service worker 發送請求 .network 請求數據(它會記錄來自該頁面加載的 .network 日志)。 當 ctrl-f3(在 chromebook 上,相當於 ctrl-f5 在 windows 上)時,然后嘗試重新加載它會要求確認刷新頁面。 如果我這樣做,然后嘗試再次刷新它,它不會顯示彈出窗口。 我做了一些調試,看到當時的window.onbeforeunload不存在。 事實上,我的部分代碼(在下面發布)沒有執行! 我將錯誤報告腳本作為外部文件加載到我的 HTML 頁面的<head>標記中。 它包裹在匿名 function 周圍,沒有 onload 偵聽器。 為什么這不起作用? 我無法全神貫注於解決方案,所以我很樂意接受幫助。 謝謝。

哦,差點忘了:這是我的代碼(縮寫為與問題相關的要點):

window.onerror = function(eventOrMessage, url, lineNumber, colNumber, error){
    window.onbeforeunload = function(){
      const channel = new BroadcastChannel("net-log");
      channel.postMessage({requestLog:true});
      channel.addEventListener("message",function(event){
        if(event.data.encodedLogEntries){
          console.log(event.data.encodedLogEntries);
        }
      });
      return "Sending messages";
    };
    //works the first time, then doesn't set. Actually, the second time it doesn't execute this at all!
    console.log(window.onbeforeunload);
}

我發現了問題,由於某種原因,這是我的服務人員的一個錯誤,導致了困境。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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