簡體   English   中英

捕獲瀏覽器關閉事件

[英]Capture browser close event

我想使用JavaScript捕獲瀏覽器關閉事件。 我用谷歌搜索,但是到處都找不到任何解決方案,下面是我在onbeforeunload處理錨,表單提交和提交按鈕的代碼。

<script>
var validNavigation = false;
function wireUpEvents() {
   window.onbeforeunload = function() {
    if (!validNavigation) {
      // invalidate session
    }
  }

   // Attach the event keypress to exclude the F5 refresh
   $(document).bind('keydown', function(e) {
     if (e.keyCode == 116){
     validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {   
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
     validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
     validNavigation = true;
  });

 }

 // Wire up the events as soon as the DOM tree is ready
 $(document).ready(function() {
  wireUpEvents();  
 });
</script>

有什么方法可以捕獲瀏覽器關閉按鈕事件,我已經看到開發人員使用X和Y軸,但是大多數開發人員不建議這樣做。

謝謝..

您可以嘗試此..提示用戶在關閉標簽頁之前進行確認。 你可以做你需要的事情

<script language="JavaScript">
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
      }
    </script>

暫無
暫無

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

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