javascript onbeforeunload disable for links

后端 未结 1 712
忘了有多久
忘了有多久 2020-12-10 19:32

I need your help. I\'m working with JavaScript and I\'m not able to configure how to work on window.onbeforeunload.

here\'s my simple code:

<         


        
相关标签:
1条回答
  • 2020-12-10 19:49

    You could try that:

    $(document).on('mousedown', 'a[href]', offBeforeUnload)
        .on('mouseleave', 'a[href]', function () {
            $(window).on('beforeunload', windowBeforeUnload);
        });
    
    
    
    function offBeforeUnload(event) {
        $(window).off('beforeunload');
    }
    
    function windowBeforeUnload() {
         if (!confirm("some message here")) { 
              return "Are you sure?"; 
          } else{ 
            return false; 
          } 
    }
    
    $(window).on('beforeunload', windowBeforeUnload);
    

    DEMO

    0 讨论(0)
提交回复
热议问题