Javascript - Confirmation when leaving page

前端 未结 2 486
我寻月下人不归
我寻月下人不归 2021-01-14 17:15

I\'m trying to implement a basic popup window that asks the user if they really want to leave a page, similar to what would happen on this site if I tried to close the windo

相关标签:
2条回答
  • 2021-01-14 18:13

    window .unload function will help us to execute some javascript while closing the browser or redirecting to any other page. resources: http://api.jquery.com/unload/

    0 讨论(0)
  • 2021-01-14 18:15

    At the head:

    <head>
    ...
    <script type="text/javascript">
    var disabledConfirm_exit=false;
    </script>
    </head>
    

    In the two links allowed to leave, you can add

    onclick="disabledConfirm_exit=true;"
    

    And inside the confirm_exit

    function confirm_exit(e) {
        if(disabledConfirm_exit) return;
        if(!e) e = window.event;
    
        e.cancelBubble = true;
        e.returnValue = 'Are you sure you want to leave?'; 
    
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    
    0 讨论(0)
提交回复
热议问题