30

在我的 HTML 页面上,当用户单击/按下F5按钮时,页面会刷新,但在刷新之前,我想执行一个函数或一个简单的警报。

用户可以单击刷新按钮,按F5Ctrl+ R

使用核心 JavaScript、jQuery 或 YUI。

4

4 回答 4

51
    window.onbeforeunload = function(event)
    {
        return confirm("Confirm refresh");
    };
于 2012-02-16T09:12:47.840 回答
8
$(window).bind('beforeunload', function(){
    return '>>>>>Before You Go<<<<<<<< \n Your custom message go here';
});

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

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

于 2012-02-16T09:12:35.500 回答
2
$(window).bind("beforeunload", function(){
        return confirm("Do you really want to refresh?"); 
});
于 2012-02-16T09:14:13.240 回答
2

jQuery 相关事件是:

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

对我很有用。

于 2017-06-29T09:30:05.023 回答