Confirm to leave the page when editing a form with jQuery

前端 未结 5 2090
无人共我
无人共我 2020-12-05 00:50

I am coding a form and I need a function like stackoverflow have: \"You have started writing or editing a post.\".

I\'ve looked through the code of stackoverflow to

5条回答
  •  失恋的感觉
    2020-12-05 01:36

    You can do this using window.onbeforeunload like this:

    window.onbeforeunload = function() {
        return 'Are you sure you want to navigate away from this page?';
    };
    

    So in your code, you would put that inside the success callback of your $.ajax call.

    To unset the confirmation, you can do this:

    window.onbeforeunload = null;
    

    Also, see this answer: How can I override the OnBeforeUnload dialog and replace it with my own?

提交回复
热议问题