how to identify onbeforeunload was caused by clicking close button

后端 未结 4 1328
囚心锁ツ
囚心锁ツ 2020-12-07 03:08

How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload?

here is a snippet:

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 03:26

    If the close button was pressed the value of e.clientY is negative. For the other possible sources i doubt there is a solution.

    window.onbeforeunload = function() {
       var e = window.event;
       alert(e.clientX + " / " + e.clientY);
    }
    

提交回复
热议问题