How do I block the [removed]('beforeUnload') event for tag?

前端 未结 2 1740
死守一世寂寞
死守一世寂寞 2020-12-10 18:25

In my project I need to get the user confirmation alert , when the user want to close the window/tab using X button.

But the window.on(\'before

2条回答
  •  再見小時候
    2020-12-10 18:39

    $(document).ready(function () {
        $('#navigate').on('mouseenter', stopNavigate)
            .on('mouseout', function () {
            $(window).on('beforeunload', windowBeforeUnload);
        });
    
    });
    
    function stopNavigate(event) {
        $(window).off('beforeunload');
    }
    
    function windowBeforeUnload() {
        return 'Are you sure you want to leave?';
    }
    
    $(window).on('beforeunload', windowBeforeUnload);
    

    DEMO

提交回复
热议问题