在“卸载”事件中停止刷新页面

时间:2010-12-29 09:47:14

标签: jquery page-refresh

如何在卸载事件时停止更新页面?

$(window).unload(function(){
    if(confirm('Do you really want to exit the survey without completing it? Your data will be deleted.') ){
         // This does not currently work in the method 
         //  onbeforeunload which took Nick Craver
            $.ajax({
                url: 'db_sotr_file.anket_my.ajax_remove_anketa',
                dataType: 'html',
                success: function(){
                    $.cookie('refresh_page', null);
                    $.cookie('hashid', null);
                    $.cookie('sessionid', null);
                }
            });            // This moment is the most important 

         // page the refresh
         return true;
    }
    // don't refresh the page
    return false;
});

1 个答案:

答案 0 :(得分:1)

您需要返回字符串并直接绑定,如下所示:

window.onbeforeunload = function() {
  return 'Do you really want to exit the survey without completing it? Your data will be deleted.';
};

为了可靠,您需要onbeforeunload而不是......并且jQuery不会绑定到这个正确的跨浏览器,因此最好直接设置事件处理程序。有些浏览器不会允许其他中的逻辑而不是return "string",只要停止页面...所以使用该格式跨浏览器工作(主要是Firefox)。

相关问题