Javascript - Confirmation when leaving page

前端 未结 2 487
我寻月下人不归
我寻月下人不归 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:15

    At the 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();
        }
    }
    

提交回复
热议问题