Execute function before refresh

一世执手 提交于 2019-11-26 17:32:59

问题


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.

User can click on refresh button, press F5 or Ctrl + R.

Using core JavaScript, jQuery or YUI.


回答1:


    window.onbeforeunload = function(event)
    {
        return confirm("Confirm refresh");
    };



回答2:


$(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




回答3:


$(window).bind("beforeunload", function(){
        return confirm("Do you really want to refresh?"); 
});



回答4:


the jQuery related event is:

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

Works great for me.



来源:https://stackoverflow.com/questions/9308336/execute-function-before-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!