刷新前执行功能

问题描述 投票:26回答:4

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

用户可以点击刷新按钮,按F5或Ctrl + R.

使用核心JavaScript,jQuery或YUI。

javascript jquery browser yui page-refresh
4个回答
40
投票
    window.onbeforeunload = function(event)
    {
        return confirm("Confirm refresh");
    };

7
投票
$(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/

但是:ぁzxswい



1
投票

jQuery相关事件是:

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

对我来说很棒。

© www.soinside.com 2019 - 2024. All rights reserved.