-1

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

$(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;
});
4

1 回答 1

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)。

于 2010-12-29T09:51:00.020 回答