[removed] executed on page refresh instead of on page close

前端 未结 4 1683
刺人心
刺人心 2020-12-19 01:41

I\'m using window.onbeforeunload to pop up a confirm dialog when a close event occurs, but the confirm dialog appears on page refresh and doesn\'t execute on pa

4条回答
  •  情书的邮戳
    2020-12-19 02:18

    No full solution, but you can intercept the F5 key (not intercepted if the user click on the refresh browser button...)

    var isRefresh = false;
    
    // with jquery
    
    $(function() {
      $(document).on("keydown", function(e) {
        if (e.which === 116)
        {
          isRefresh = true;
        }
      });
    });
    
    window.onbeforeunload = function() {
     if (! isRefresh)
     {
       return confirm ('Close ?');
     }
     return false;
    };
    

提交回复
热议问题