Execute function before refresh

前端 未结 4 1869
深忆病人
深忆病人 2020-11-29 06:40

On my HTML page, when the user clicks / presses F5 button the page refreshes, but before it refreshes I want to execute a function or a simple alert.

Use

相关标签:
4条回答
  • 2020-11-29 07:06
    $(window).bind('beforeunload', function(){
        return '>>>>>Before You Go<<<<<<<< \n Your custom message go here';
    });
    

    Source: http://www.mkyong.com/jquery/how-to-stop-a-page-from-exit-or-unload-with-jquery/

    Demo: http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html

    0 讨论(0)
  • 2020-11-29 07:11
        window.onbeforeunload = function(event)
        {
            return confirm("Confirm refresh");
        };
    
    0 讨论(0)
  • 2020-11-29 07:18
    $(window).bind("beforeunload", function(){
            return confirm("Do you really want to refresh?"); 
    });
    
    0 讨论(0)
  • 2020-11-29 07:20

    the jQuery related event is:

    $( window ).on('unload', function( event ) {
        console.log('Handler for `unload` called.');
    });
    

    Works great for me.

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